Anda di halaman 1dari 39

CLASS AND OBJECT IMPLEMENTATION

#include<iostream.h>
#include<string.h>
#include<conio.h>
class student
{
int regno;
char name[20];
public:
void setdata(int r, char *n);
void putdata()
{
cout<<"\n \n regno "<<regno;
cout<<"\n Name "<<name;
}
};
void student::setdata(int r, char *n)
{
regno=r;
strcpy(name,n);
}
int main()
{
student s1,s2;
clrscr();
s1.setdata(100,"ajay");
s2.setdata(101,"vijay");
s1.putdata();
s2.putdata();
getch();
return 0;
}
OUTPUT:
Regno 100
Name ajay
Regno 101
Name vijay

IMPLEMENTATION OF FUNCTIONS WITH DEFAULT ARGUMENTS

#include <iostream.h>
#include<conio.h>
void f(int a=0, int b=0)
{
cout << "\ta:" << a << "\tb:" << b;
cout << '\n';
}
int main()
{
clrscr();
f ();
f (10);
f(10, 99);
getch();
return 0;
}
OUTPUT:
Function f()
a:0

b:0

Function f(10)
a:10

b:0

Function f(10,99)
a:10

b:99

PROGRAM FOR CALL BY VALUE


#include <iostream.h>
#include<conio.h>
void doubleIt(int);

int main ()
{
int num;
clrscr();
cout << "Enter number: ";
cin >> num;
doubleIt(num);
cout << "The number doubled in main is " << num << endl;
getch();
return 0;
}
void doubleIt (int x)
{
cout << "The number to be doubled is " << x << endl;
x *= 2;
cout << "The number doubled in doubleIt is " << x << endl;
}
OUTPUT:
Enter number:
3
The number to be doubled is:3
The number doubled in doubleIt is6
The number doubled in main is:3
PROGRAM FOR CALL BY REFERENCE
#include <iostream.h>
#include <conio.h>
void multi(int *);
int a,b,c;
int main()

{
clrscr();
cout<<"Enter the a value:\n";
cin>>a;
multi(&a);
cout<<"\n Multiplication of a and b in main function is:"<< c;
getch();
return 0;
}
void multi(int *p)
{
cout<<"Enter the b value:\n";
cin>>b;
c=((*p)*b);
cout<<"\n Multiplication of a and b in multi function is:"<< c;
}
OUTPUT:
Enter a value: 34
Enter b value: 23
Multiplication of a and b in multi function is: 782
Multiplication of a and b in main function is: 782
PROGRAM TO ADD TWO COMPLEX NUMBERS USING BINARY OPERATOR
OVERLOADING

#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"\nEnter the value of Complex Numbers a,b:";
cin>>a>>b;

}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values are:\n";
obj1.display();
obj2.display();
cout<<"Result calculated are:\n";
result.display();
result1.display();
getch();
}

OUTPUT:
Enter the value of Complex Numbers a, b
45
Enter the value of Complex Numbers a, b
22
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i

PROGRAM TO CALCULATE PRIME NUMBER USING


CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
void calculate()
{
k=1;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
{
k=0;
break;
}

else
k=1;
}
}
void show()
{
if(k==1)
cout<<"The given number is prime.\n";
else
cout<<"The given number is not prime.\n";
}
};
void main()
{
int a,b;
clrscr();
for(b=0;b<=2;b++)
{
cout<<"Enter the Number:\n";
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
}
getch();
}
OUTPUT:
Enter the number: 7
Given number is prime.
Enter the number: 10
Given number is not prime.

PROGRAM TO CALCULATE FACTORIAL OF A GIVEN NUMBER USING


COPY CONSTRUCTOR
#include<iostream.h>

#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
int calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
int n;
clrscr();
cout<<"\nEnter the Number:";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n"<<"\nFactorial is:"<<obj.calculate();
cout<<"\n"<<"\nFactorial is:"<<cpy.calculate();
getch();
}

OUTPUT:
Enter the Number: 5
Factorial is: 120

Factorial is: 120

PROGRAM TO FIND THE MEAN VALUE OF A GIVEN NUMBER


USING FRIEND FUNCTION.
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is : "<<mean(obj);

getch();
}
OUTPUT:
Enter two values: 10, 20
Mean Value is: 15

PROGRAM TO CALCULATE THE AREA OF CIRCLE, RECTANGLE


AND TRIANGLE USING FUNCTION OVERLOADING
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define PI 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:\n"<<PI*a*a<<"\n";
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:\n"<<a*b<<"\n";
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:\n"<<t*a*b<<"\n";
}
void main()
{
int ch,a,b,r;

fn obj;
clrscr();
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle \n2.Area of Rectangle \n3.Area of Triangle \n4.Exit\n";
while(1)
{
cout<<"Enter your Choice:\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of the Circle:\n";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:\n";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:\n";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
}
OUTPUT:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit

Enter Your Choice: 1


Enter the Radius of the circle: 2
Area of the Circle: 12.56
Enter Your Choice: 2
Enter sides of the Rectangle: 5 5
Area of Rectangle is: 25
Enter Your Choice: 3
Enter sides of the Triangle: 2 3
Area of Triangle: 3
Enter Your Choice: 4

PROGRAM TO SWAP THE NUMBERS USING THE CONCEPT OF


FUNCTION TEMPLATE
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t &y)
{
t temp;
temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout<<"\na and b before swapping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swapping :"<<a<<"\t"<<b;
cout<<"\n \n";
cout<<"\nc and d before swapping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swapping :"<<c<<"\t"<<d;
}
void main()
{

int a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
OUTPUT:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50

PROGRAM TO FIND THE MULTIPLICATION VALUES AND THE


CUBIC VALUES USING INLINE FUNCTION
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};

void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\multiplication value is:"<<obj.mul(val1,val2)<<"\n";
cout<<"\nCube value are :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
OUTPUT:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 125 343

PROGRAM TO FIND OUT THE PAYROLL SYSTEM USING SINGLE


INHERITANCE
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp

{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Human Resource Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitability Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t
"<<np<<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employees:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";

for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
OUTPUT:
Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No E.name des BP HRA DA PF NP
150 ram Manager 5000 1000 500 300 6200
PROGRAM TO FIND THE COMPLEX NUMBERS USING UNARY OPERATOR
OVERLOADING

#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c;
public:
complex()
{
}
void getvalue()
{
cout<<"Enter two numbers:\n";

cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
b=--b;
}
void display()
{
cout<<a<<"+\t"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj;
obj.getvalue();
obj++;
cout<<"\nIncrement Complex Number\n";
obj.display();
obj--;
cout<<"\nDecrement Complex Number\n";
obj.display();
getch();
}
OUTPUT:
Enter the two numbers: 3 6
Increment Complex Number
4 + 7i
Decrement Complex Number

3 + 6i

PROGRAM FOR VIRTUAL FUNCTIONS


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show function:";
}
void display()
{
cout<<"\n Base class display function:" ;
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n Derive class display function:";
}
void show()
{
cout<<"\n Derive class show function:";
}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base class:\n" ;
p=&obj1;

p->display();
p->show();
cout<<"\n\n\t P points to derived class:\n";
derived obj2;
p=&obj2;
p->display();
p->show();
getch();
}
OUTPUT:
P points to Base class
Base class display function
Base class show function
P points to Derived class
Base class display function
Drive class show function

PROGRAM FOR FACTORIAL USING RECURSION


#include<iostream.h>
#include<conio.h>
void main()
{
int n,fact;
int rec(int);
clrscr();
cout<<"Enter the number:->";
cin>>n;
fact=rec(n);
cout<<endl<<"Factorial Result are:: "<<fact<<endl;
getch();
}
rec(int x)
{
int f;

if(x==1)
return(x);
else
{
f=x*rec(x-1);
return(f);
}
}
OUTPUT:
Enter the number: 5
Factorial Result are: 120

PROGRAMS USING FUNCTIONS WITH DEFAULT ARGUMENTS

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,m,n;
int sq(int m,int n=2);
clrscr();
cout<<"\n enter the datas:";
cin>>m>>n;
x=sq(m,n);
cout<<"\n"<<x;
y=sq(m);
cout<<"\n"<<y;
getch();
}
int sq(int a,int b)
{
if(b==2)
return (a*a);
else
return (sq(a,b-1)*a); //Recursive function
//cout<<"\n result:"<<(pow(a,b)); //Library function

IMPLEMENTATION OF CALL BY VALUE, CALL BY ADDRESS

#include<iostream.h>
#include<conio.h>
void swap(int,int);
void swap(int*,int*);
void main()
{
int ch,a,b;
char ch1;
clrscr();
cout<<"\n enter 2 values:";
cin>>a>>b;
do
{
cout<<"\n swapping menu!!! 1. callbyvalue 2. callbyreference ";
cout<<"\n select ur choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n *****call by value*****";
cout<<"\n before swapping:"<<a<<b;
swap(a,b);
cout<<"\n after swapping:"<<a<<b;
break;
case 2:
cout<<"\n *****call by reference*****";
cout<<"\n before swapping:"<<a<<b;
swap(&a,&b);
cout<<"\n after swapping:"<<a<<b;
break;
default:
break;
}
cout<<"\n want to continue?? (y or n)";

cin>>ch1;
}while(ch1=='y'|ch1=='Y');
getch();
}
void swap(int m,int n)
{
int t;
t=m;
m=n;
n=t;
}
void swap(int *m,int *n)
{
int t;
t=*m;
*m=*n;
*n=t;
}
CLASSES WITH PRIMITIVE DATA MEMBERS

#include<iostream.h>
#include<string.h>
#include<conio.h>
class employee
{
int empno,len;
char *empname;
public:
float basic,hra,da,pf,net;
employee(int x,char *y,int z)
{
len=0;
empno=x;
len=strlen(y);
empname=new char[len+1];
strcpy(empname,y);
empname=y;
basic=z;

}
void display()
{
cout<<"\n Output:\n";
cout<<"Employee Number:"<<empno;
cout<<"\nEmployee Name:"<<empname;
cout<<"\nEmployee Salary:"<<basic;
}
void showsal()
{
cout<<"\nHRA:"<<hra;
cout<<"\nDA:"<<da;
cout<<"\nPF:"<<pf;
cout<<"\n NET SALARY:"<<net;
}
void netsalary()
{
hra=basic*0.3;
da=basic*0.2;
pf=basic*0.1;
net=(basic+hra+da)-pf;
}
};
void main()
{
int x,z;
char y[30];
clrscr();
cout<<"\n INPUT:\n";
cout<<"Enter the employee number:";
cin>>x;
cout<<"Enter the employee name:";
cin>>y;
cout<<"Enter the basic salary:";
cin>>z;
employee e(x,y,z);
e.netsalary();
e.display();
e.showsal();

getch();
}
CLASSES WITH ARRAYS AS DATA MEMBERS

#include<iostream.h>
#include<conio.h>
int x,y;
//global declaration
class matrix
{
int a[5][5];
public:
void get();
void print();
matrix operator+(matrix);
};
matrix matrix::operator+(matrix m2) //using operator overloading
{
matrix m3;
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
m3.a[i][j]=a[i][j]+m2.a[i][j];
return m3;
}
void matrix::get()
{
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++)
cin>>a[i][j];
}
void matrix::print()
{
for(int i=1;i<=x;i++)
{
cout<<endl;
for(int j=1;j<=y;j++)
cout<<a[i][j]<<"\t";
}
}

void main()
{
matrix m1,m2,m3;
clrscr();
cout<<"Enter the matrix size:";
cin>>x>>y;
if(x==y)
{
cout<<"Enter the 1st matrix:";
m1.get();
cout<<"Enter the 2nd matrix:";
m2.get();
m3=m1+m2;
m3.print();
}
else
cout<<"matrix addition can not be performed";
getch();
}
CLASSES WITH POINTERS AS DATA MEMBERS

#include<iostream.h>
#include<conio.h>
#include<string.h>
class concate
{
char *str1,*str2;
public:
concate(char *string1,char *string2)
{
str1=new char(strlen(string1)+strlen(string2));
str1=string1;
str2=string2;
}
void display()
{
cout<<"\nThe concatenation of two string: ";
cout<<"\n result:"<<strcat(str1,str2);

}
~concate()
{
delete[]str1;
delete[]str2;
cout<<"\n String is deleted!"<<"\n";
}
};
void main()
{
char *str1,*str2;
clrscr();
cout<<"\n enter 1st string:";
cin>>str1;
cout<<"\n enter 2nd string:";
cin>>str2;
concate obj(str1,str2);
obj.display();
getch();
}
CLASSES WITH CONSTANT DATA MEMBERS

#include<iostream.h>
#include<conio.h>
#include<string.h>
class employee
{
char *name,*no,*add;
public:
void init();
void clear();
void setname(char const*str);
void setno(char const*str);
void setadd(char const*str);
char const*getname(void)const; //Constant return type & Constant function
char const*getno(void)const;
char const*getadd(void)const;
};

void employee::init()
{
name=no=add=0;
}
void employee::clear()
{
delete name;
delete add;
delete no;
}
void employee::setname(char const*str)
{
if(name)
delete name;
name=new char[strlen(str)+1];
strcpy(name,str);
}
void employee::setno(char const*str)
{
if(no)
delete no;
no=new char[strlen(str)+1];
strcpy(no,str);
}
void employee::setadd(char const*str)
{
if(add)
delete add;
add=new char[strlen(str)+1];
strcpy(add,str);
}
char const*employee::getname()const
{
return name;
}
char const*employee::getno()const
{
return no;
}
char const*employee::getadd()const

{
return add;
}
void printemployee(employee e)
{
if(e.getname())
cout<<"\n"<<e.getname()<<endl;
if(e.getno())
cout<<"\n"<<e.getno()<<endl;
if(e.getadd())
cout<<"\n"<<e.getadd()<<endl;
}
void main()
{
employee e[10];
int i,n;
char *ch1,*ch2,*ch3;
clrscr();
cout<<"\n enter the no of details:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n";
e[i].init();
cout<<"\n enter the name:";
cin>>ch1;
e[i].setname(ch1);
cout<<"\n enter no:";
cin>>ch2;
e[i].setno(ch2);
cout<<"\n enter add:";
cin>>ch3;
e[i].setadd(ch3);
}
for(i=0;i<n;i++)
{
cout<<"\n";
printemployee(e[i]);
e[i].clear();
}

getch();
}
CLASSES WITH STATIC MEMBER FUNCTIONS

#include<iostream.h>
#include<conio.h>
class test
{
private:
int code;
static int count;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout <<"object number:"<<CODE<<ENDL;
}
static void showcount()
{ cout<<"\n count:"<<COUNT<<ENDL;
}
};
int test::count;
void main()
{
test t1,t2;
clrscr();
cout<<"\n\t\t Program for class with static data members\n";
t1.setcode();
test::showcount();
t2.setcode();

test::showcount();
test t3;//Object creation
t3.setcode();
test::showcount();
cout<<"\n\n";
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
FUNCTION OVERLOADING

#include<iostream.h>
#include<conio.h>
void complexmul(int,int);
void complexmul(int,int,float,float);
void complexmul(float,float,float,float);
void main()
{
int e,f;
float a,b,c,d;
void print(float,float,float,float);
clrscr();
cout<<"\nFUNCTION OVERLOADING";
cout<<"\nEnter the integer input:";
cin>>e>>f;
cout<<"\nEnter the float input:";
cin>>a>>b>>c>>d;
cout<<"\nOutput value:";
cout<<"\nThe 1st Function output:";
print(e,f,e,f);
complexmul(e,f);
cout<<"\nThe 2nd Function output:";
print(e,f,a,b);
complexmul(e,f,a,b);
cout<<"\nThe 3rd Function output:";
print(a,b,c,d);

complexmul(a,b,c,d);
getch();
}
void print(float re1,float im1,float re2,float im2)
{
cout<<"\n"<<"("<<re1<<"+i"<<im1<<")"<<"*"<<"("<<re2<<"+i"<<im2<<")"<<"
=>";
}
void complexmul(int e,int f)
{
int r1,i1;
r1=(e*e)-(f*f);
i1=2*(e*f);
cout<<r1<<"+j"<<i1;
}
void complexmul(int e,int f,float a,float b)
{
float r2,i2;
r2=(e*a)-(f*b);
i2=(e*b)+(f*a);
cout<<r2<<"+j"<<i2;
}
void complexmul(float a,float b,float c,float d)
{
float r3,i3;
r3=(a*c)-(b*d);
i3=(a*d)+(b*c);
cout<<r3<<"+j"<<i3;
}

MANAGING BANK ACCOUNT USING INHERITANCE CONCEPT

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
long int bal=0;
int i=0,n=0;

class bank
{
public:
char *cname,*acno;
void display()
{
cout<<"\nMr:"<<cname;
cout<<"\nACCno:"<<acno;
}
};
class savings : public bank
{
public:
long int wdraw,dep,x[10],init_dep[10];
void saves()
{
n++;
cout<<"Enter the amount to be deposited=Rs.";
cin>>dep;
init_dep[n]=dep;
bal+=dep;
}
void withdraw()
{
i++;
cout<<"Your A/c Balance is=Rs."<<bal;
cout<<"\nEnter the amount to be withdrawn=Rs.";
cin>>wdraw;
x[i]=wdraw;
bal-=wdraw;
cout<<"Your Current A/c Balance is=Rs."<<bal<<endl;
}
};
class current : public savings
{
public:
void info();
};
void current::info()
{

long int init_tot=0;


display();
cout<<"\nDeposited amount=Rs.";
for(int k=1;k<=n;k++)
{
cout<<init_dep[k]<<"+";
init_tot=init_tot+init_dep[k];
}
cout<<"=>"<<init_tot<<endl;
for(int j=1;j<=i;j++)
cout<<j<<".withdrawn=Rs."<<x[j]<<endl;
cout<<"Your A/c Balance is=Rs."<<bal<<endl;
}
void main()
{
int ch;
current ac;
clrscr();
cout<<"Enter customer name:";
cin>>ac.cname;
cout<<endl<<"Enter your A/c no.:";
cin>>ac.acno;
cout<<endl;
while(1)
{
cout<<"\nSelect the option,1.Savings 2.With draw 3.Mini statement 4.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
ac.saves();
break;
case 2:
ac.withdraw();
break;
case 3:
ac.info();
break;
case 4:
exit(0);

}
}
getch();
}
VIRTUAL FUNCTION

#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void getdata()
{}
virtual void display()
{}
};
class triangle:public shape
{
int h,bs;
float at;
public:
void getdata()
{
cout<<"Enter the height and base of triangle = ";
cin>>h>>bs;
}
areatri()
{
at=((bs*h)/2.0);
return (at);
}
void display()
{
cout<<"Area of Triangle = "<<at<<" Sq.Units"<<endl;
}
};
class rectangle:public shape

{
int l,b,ar;
public:
void getdata()
{
cout<<"Enter the length and breadth of rectangle = ";
cin>>l>>b;
}
arearec()
{
ar=(l*b);
return(ar);
}
void display()
{
cout<<"Area of Rectangle = "<<ar<<" Sq.Units"<<endl;
}
};
void main()
{
clrscr();
shape s;
triangle t;
rectangle r;
shape *bptr;
cout<<"For Triangle"<<endl;
bptr=&t;
bptr->getdata();
t.areatri();
bptr->display();
cout<<"For Rectangle"<<endl;
bptr=&r;
bptr->getdata();
r.arearec();
bptr->display();
getch();
}

VIRTUAL BASE CLASSES

class student
{
protected:
int no;
char name[20];
public:
void getstud()
{
cin>>no>>name;
}
void putstud()
{
cout<<no<<name;
}
};
class mark: virtual public student
{
protected:
int m1,m2;
public:
void getm()
{
cin>>m1>>m2;
}
void putm()
{
cout<<m1+m2;
}
};
class sports: virtual public student
{
protected:
int scr;
public:
void get_s()
{
cin>>scr;

}
void put_s()
{
cout<<scr;
}
};
class display: public mark, public sports
{
public:
void print()
{
putstud();
putm();
put_s;
}
};
void main()
{
display d;
d.getstud();
d.getm();
d.get_s();
d.print();
getch();
}
OVERLOADING NEW AND DELETE OPERATORS
#include<iostream.h>
const int SIZE=10;
class vector
{
private:
int *array;
public:
void *operator new(size_t s)
{

vector *my_vector;
my_vector=::new vector;
my_vector->array=new int[SIZE];
return my_vector;
}
void operator delete(void *vec)
{
Vector *my_vect;
my_vect=(vector *) vec;
delete(int *)my_vect->array;
::delete vec;
}
void read();
int sum();
};
Void vector::read()
{
for(int i=0;i<SIZE;i++)
{
cout<<vector[<<i<<]=?;
cin>>array[i];
}
}
int vector::sum()
{
int sum=0;
for(int i=0;i<SIZE;i++)
sum=sum+array[i];
return sum;
}
void main()

{
vector *my_vector=new vector;
cout<<enter vector data..<<endl;
My_vector->read();
cout<<sum of vector=<<my_vector->sum();
delete my_vector;
}
Output:
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:1
vector[0]=?:2
sum of vector=10