Monday, November 2, 2009

// constructos in Derived class c++


#include"iostream.h"

class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"alpha initialized \n";
}
void show_x(void)
{
cout<<"x="< }
};

class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"beta initialized \n";
}
void show_y(void)
{
cout<<"y="< }
};

class gamma:public beta,public alpha
{
int m,n;
public:
gamma(int a, float b, int c, int d):
alpha(a),beta(b)
{
m=c;
n=d;
cout<<"gamma initialized \n";
}
void show_mn(void)
{
cout<<"m="< }
};

int main()
{
gamma g(5,10.75,20,30);
cout<<"\n";
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

//OverLoading Unary Minus c++


#include"iostream.h"
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-(); //overloaded unary minus
};

void space::getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout< cout< cout<}
void space:: operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space S;
S.getdata(10,-20, 30);
cout<<"S:";
S.display();
-S; //activates operator-() function

cout<<"S:";
S.display();
return 0;
}

//static member function usin c++


#include"iostream.h"

class test
{
int code;
static int count; //static member variable

public:

void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"< }
static void showcount(void) //static member funcion
{
cout<<"count:"< }
};

int test::count;

int main()
{
test t1,t2;
t1.setcode();
t2.setcode();

test::showcount(); //accessing static function

test t3;
t3.setcode();

test::showcount();

t1.showcode();
t2.showcode();
t3.showcode();

return 0;
}

// Objects as Arguments usin c++


#include "iostream.h"
#include"conio.h"

class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout< cout< }

void sum(time,time); //declaration with objects as arguments
};

void time::sum(time t1, time t2) //t1,t2 are objects
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}

int main()
{
time T1,T2,T3;

T1.gettime(2,45); //get T1

T2.gettime(3,40); //get T2

T3.sum(T1,T2); //T3=T1+T2

cout<<"T1=";
T1.puttime(); //display T1

cout<<"T2=";
T2.puttime(); //display T2

cout<<"T3=";
T3.puttime(); //display T3

getch();

return 0;
}

friend function using c++

#include"iostream.h"

class sample
{
int a;
int b;
public:
void setvalue() {a=50; b=40; }

friend float mean(sample s);
};


float mean(sample s)
{
return float(s.a+s.b)/2.0;
}


int main()
{
sample X; //object X
X.setvalue();

cout<< "Mean value="<
return 0;
}

// OverLoading + Operator c++

#include"iostream.h"
#include"conio.h"
class complex
{
float x; //real part
float y; // imaginarypart
public:
complex(){ } //constructor 1
complex(float real, float imag) //constructor 2
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp; //temporary
temp.x=x+c.x;
temp.y=y+c.y; // these are float additions
return(temp);
}
void complex::display(void)
{
cout<}
int main()
{
complex C1,C2,C3;
clrscr(); //invokes constructor 1
C1=complex(2.5, 3.5);
C2=complex(1.6,2.7); //invokes constructor 2
C3=C1+C2;
cout<<"C1="; C1.display();
cout<<"C2="; C2.display();
cout<<"C3'"; C3.display();
return 0;
}

Arrays of Objects usin c++


#include "iostream.h"
#include"conio.h"

class employee
{
char name[30]; //string as class member
float age;
public:
void getdata(void);
void putdata(void);
};

void employee::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}

void employee::putdata(void)
{
cout<<"Name:"< cout<<"Age:"<
}
const int size=3;

int main()
{
employee manager[size];
clrscr();
for(int i=0;i {
cout<<"\n Details of manager"< manager[i].getdata();
}

cout<<"\n";

for(i=0;i {
cout<<"\n Manager"< manager[i].putdata();
}

getch();

return 0;

}