Thursday, July 27, 2006

C++ basic programs

C++ - basics through simple programs

1) Program to illustrate memory allocation for objects

This is a program to illustrate, member functions [non virtual ] member functions are allocated in the code segment and not relative to the object.

#include <iostream.h>
using namespace std;

class corrupt{
int data;
public:
corrupt()
{
data=0;
}
void print()
{
cout<<"Print Function called \n";
// cout<<data;
}
};

int main ()
{
corrupt *p;
p =new corrupt;
p =NULL;
p->print();
}

Uncommenting the bold line would lead to segmentation violation, otherwise the program works fine though we have made object p to NULL. When the line in bold is uncommented obviously the function tries to dereference the this pointer and thus leads to segmentation fault.

Output:
Print Function called

2) Program to illustrate copy constructors

#include <iostream.h>
#include <string>
using namespace std;


class car {

friend ostream & operator<<(ostream &os, car &c);

int wheels;
string model;
int price;
public:
car(int w, char *m, int p)
{
wheels=w;
model=m;
price=p;
}
car (car &c)
{
cout<< "Copy constructor called here\n";
model= "copy of " + c.model;
wheels= c.wheels;
price= c.price;
}
};

ostream & operator<<(ostream &os, car &c)
{
os <<"Car model name :"<<c.model<<endl;
os <<"Car price :"<<c.price<<endl;
os <<"Car wheels :"<<c.wheels<<endl;
return os;
}

void function (car c1, car c2)
{
cout<<c1;
cout<<c2;
}

int main()
{
car c1(4,"VW passat",14000);
car c2(4,"Toyota Yaaris",14000);
cout<<c1<<c2;
function (c1,c2);
}

Output:
Car model name :VW passat
Car price :14000
Car wheels :4
Car model name :Toyota Yaaris
Car price :14000
Car wheels :4
Copy constructor called here
Copy constructor called here
Car model name :copy of VW passat
Car price :14000
Car wheels :4
Car model name :copy of Toyota Yaaris
Car price :14000
Car wheels :4

3) Operator overloading

#include <iostream.h>

class vehicle {
int manucost;
int regn;
int tax;
public:
vehicle (int a, int b, int c)
{
manucost=a;
regn=b;
tax=c;
}
int operator+( vehicle &v1)
{
return (manucost + regn + tax + v1.manucost + v1.regn + v1.tax );
}
};

int main()
{
vehicle v1(10,1,1);
vehicle v2(20,2,2);
int tc= v1+v2;
cout<< "Total cost of the vehicle is :"<<tc<<endl;
}

Output:
Total cost of the vehicle is :36

4) Templates

#include <iostream.h>
#include <string>
using namespace std;

template <class T> class array {
int currIndex;
int MaxElem;
int Size;
class T *p;
public:
array (int c, int m, int s)
{
currIndex=c;
MaxElem=m;
Size=s;
cout << "Constructor template \n";
p = new T [s];
}
arrayprint()
{
for (int i=0;i<=Size;i++)
{
cout<<p[i];
}
}
};

class myClass{
friend ostream & operator << (ostream &, myClass &);
string *p;
public:
myClass()
{
p = new string ("Hello World\n");
cout<<*p;
}
};

ostream& operator << (ostream &os, myClass &my){
os << my.p;
return os;
}

int main( int argc, char * argv[])
{
array<myClass> p(4,5,7);
return 1;
}

Output:

Constructor template
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

5) Using STL containers

#include <iostream.h>
#include <vector>
using namespace std;


class Names{
friend ostream & operator << (ostream &, Names &);
int number;
public:
Names(int a)
{
number=a;
}
int display()
{
return number;
}
};

vector <Names>v;

ostream & operator << (ostream &os, Names &name){
os<<name.number;
return os;
}

int main ()
{
// append elements with values 1 to 6
for (int i=1; i<=6; ++i) {
v.push_back(i);
}

for (vector<Names>::iterator it=v.begin(); it!=v.end(); ++it) {
cout<<"Object value :"<<*it<< endl;
}

}

Output:

Object value :1
Object value :2
Object value :3
Object value :4
Object value :5
Object value :6

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home