Monday, September 13, 2010

How to post long C/C++ code in blog?

Insert the following code in your "Edit HTML",
go to Compose, write your C++ code.

<pre class="alt2" dir="ltr" style="border: 1px inset; height: 498px; margin: 0px; overflow: auto; padding: 6px; text-align: left; width: 640px;"> </pre>
Example below:

Following codes are old codes, mixed with C and C++.
12-1. An example of ifstream and ofstream in c++


#include < iostream.h >
#include < stdlib.h   >
#include < fstream.h  >
void main(int argc, char **argv)
{
        char buffer[1];
        ifstream input(argv[1],ios::in);
        if(input.fail())
        {
                cout<<"Error opening the file"< < argv[1];
                exit(1);
        }
        ofstream output(argv[2],ios::out);
        if(out.fail())
        {
                cout<<"Error opening the file"< < argv[1];
                exit(1);
        }
        do {
                input.read(buffer,sizeof(buffer));
                if(input.good())
                        output.read(buffer,sizeof(buffer));
        } while(!input.eof());
        input.close();
        output.close();
}



12. An example of new and delete in c++

#include < iostream.h >
#include < stdlib.h   >
#include < string.h  >
class Book
{
        public:
                void show_title(void) {cout<< title<< endl;};
                void show_book(void)
                {
                        show_title();
                        show_publisher();
                };
                Book(char *title, char *author, char *publisher, float price);
                ~Book(void) {cout< < "Destroying the entry for"<< title << endl;};
        private:
                char title[256];
                char author[64];
                float price;
                char publisher[256];
                void show publisher(void) {cout << publisher << endl;};
};
Book::Book(char *title, char *author, char *publisher, float price)
{
        strcpy(Book::title, title);
        strcpy(Book::author,author);
        strcpy(Book::publisher,publisher);
        Book::price = price;
}
void main(void)
{
        Book *Library[4];
        int i =0;
        Library[0] = new Book("t0","a0","p0",0.0);
        Library[1] = new Book("t1","a1","p1",1.0);
        Library[2] = new Book("t2","a2","p2",2.0);
        Library[3] = new Book("t3","a3","p3",3.0);
        for(i=0; i<4;i++)
                Library[i]->show_book();
        for(i=0;i<4;i+=)
                delete Library[i];
}

13. Using namespace

A namespace is a technique of grouping some functions and/or classes to create an entity.
This has the main goal of eliminating the probability of having two functions or two classes 
with the same name. For example, if different people work on the same project, each one can
 create his or her own classes but put them in only his or her namespace. This way, even if 
they happen to have a class each with the same name, there is no risk of having a name conflict.
define namespace:
namespace limited{
int i,k;
void sample(int j) {cout < < j < < endl;}
}
Using namespace:
using limited::k; //only make k visible
k=10;
using namespace limited;//makes entire limited namespace visible
k=10;
using namespace std; //using standard namespace

14 The three basic principles of object-oriented design is:

a) Encapsulation—The ability to combine data and operations on that data in a single unit.
b) Inheritance—The ability to create new objects from existing objects.
c) Polymorphisms-the ability to use the same expression to denote different operations.


No comments:

Post a Comment