OOPS prog
SIMPLE CLASS
DESIGN
#include<iostream.h>
#include<conio.h>
class
add
{
int
a,b;
public:
void getvalue()
{
cout<<"\nEnter
two numbers:";
cin>>a>>b;
}
void
printresult()
{
cout<<"SUM
= "<<(a+b);
}
};
void
main()
{
clrscr();
add
a;
a.getvalue();
a.printresult();
getch();
}
DYNAMIC MEMORY
ALLOCATION
#include<iostream.h>
#include<string.h>
#include<conio.h>
class
string
{
char
*name;
int
length;
public:
string()
{
length=0;
name=new char[length+1];
}
string(char *s)
{
length = strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<"\n\t"<<name;
}
void join(string &a, string &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name, a.name);
strcat(name, b.name);
}
};
void
main()
{
string s1,s2,s3;
clrscr();
string name1("DMI"),
name2("EEE"), name3("DEPT"), name4("ROCKS");
cout<<"\n\n\t\t**** STRING
CONCATENATION****"<<"\n\n";
name1.display();
name2.display();
name3.display();
name4.display();
s1.join(name1,name2);
s2.join(s1,name4);
s3.join(name2,name4);
s1.display();
s2.display();
s3.display();
getch();
}
CONSTRUCTOR AND
DESTRUCTOR
#include<iostream.h>
#include<conio.h>
int
count=0;
class
matrix
{
public:
matrix();
~matrix();
};
matrix::matrix()
{
count++;
cout<<"\nObject "
<<count<<" Constructor of class test is called";
}
matrix::~matrix()
{
cout<<"\nObject "
<<count<<" Destructor of class test is called";
count--;
}
void
main()
{
clrscr();
{
matrix x;
matrix y;
}
getch();
}
COPY CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class
matrix
{
int
id;
public:
matrix()
{
}
matrix(int a)
{
id=a;
}
matrix(matrix &x)
{
id=x.id;
}
display()
{
cout<<id;
return 0;
}
};
void
main()
{
clrscr();
matrix
a(100);
matrix
b(a);
matrix
c=a;
matrix=d;
d=a;
cout<<"\n
ID of A object is:";
a.display();
cout<<"\n
ID of B object is:";
b.display();
cout<<"\n
ID of C object is:";
c.display();
cout<<"\n
ID of D object is:";
d.display();
getch();
}
OPERATOR
OVERLOADING
#include<iostream.h>
#include<conio.h>
class
complex
{
private:
float real;
float imag;
public:
complex()
{
real=imag=0;
}
void
getdata()
{
cout<<"Real part?";
cin>>real;
cout<<"Imag part?";
cin>>imag;
}
complex
operator+(complex c2);
void
outdata(char *msg)
{
cout<<endl<<msg;
cout<<"("<<real;
cout<<","<<imag<<")";
}
};
complex
complex::operator+(complex c2)
{
complex
temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
void
main()
{
clrscr();
complex
c1,c2,c3;
cout<<"Enter
complex number c1..."<<endl;
c1.getdata();
cout<<"Enter
complex number c2..."<<endl;
c2.getdata();
c3=c1+c2;
c3.outdata("c3=c1+c2:");
getch();
}
FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>
class
distance1;
class
distance
{
private:
int dist;
public:
distance()
{
dist=10;
}
void showdist()
{
cout<<dist;
}
friend
distance add(distance,distance1);
};
class
distance1
{
private:
int dist1;
public:
distance1()
{
dist1=20;
}
void showdist1()
{
cout<<dist1;
}
friend
distance add(distance,distance1);
};
int
distance add(distance aa,distance1 bb)
{
return
(aa.dist+bb.dist1);
}
void
main()
{
distance
d;
clrscr();
distance1.d1;
cout<<"\n
Distance of class 1 object=";
d.showdist();
cout<<"\n
Distance of class 2 object=";
d1.showdist1();
cout<<"\n
Addition of two distances =";
cout<<distanceadd(d,d1);
getch();
}
RUN TIME
POLYMORPHISM:
#include<iostream.h>
#include<conio.h>
class
base
{
private:
int x;float y;
public:
virtual void
getdata();
virtual void
display();
};
class
derivedB:public base
{
private:
long int rollno;
char name[20];
public:
void getdata();
void display();
};
void
base::getdata()
{
cout<<"enter
an integer\n";
cin>>x;
cout<<"enter
a real no\n";
cin>>y;
}
void
base::display()
{
cout<<"entered
nos are x="<<x<<"&y="<<y;
cout<<endl;
}
void
derivedB::getdata()
{
cout<<"enter
rollno of student\n";
cin>>rollno;
cout<<"enter
name of student\n";
cin>>name;
}
void
derivedB::display()
{
cout<<"rollno"<<rollno<<"\t"<<"name"<<name<<endl;
}
void
main()
{
clrscr();
base
*ptr;
derivedB
obj;
ptr=&obj;
ptr->getdata();
ptr->display();
getch();
}
OVERLOADING
ASSIGNMENT OPERATOR
#include<iostream.h>
#include<conio.h>
class
sample
{
private:
int x;
float y;
public:
sample(int,float);
void operation=(sample abc);
void display();
};
sample::sample(int
one,float two)
{
x=one;
y=two;
}
void
sample::operator=(sample abc)
{
x=abc.x;
y=abc.y;
}
void
sample::display()
{
cout<<"integer
number[x]="<<x<<endl;
cout<<"float
value[y]="<<y<<endl;
cout<<endl;
}
void
main()
{
clrscr();
sample
obj1(10,-22.55);
sample
obj2(20,-33.44);
obj1.operator=(obj2);
cout<<"contents
of 1st object\n";
obj.display();
cout<<"contents
of 2nd object\n";
obj2.display();
getch();
}
TEMPLATE
#include<iostream.h>
#include<conio.h>
template<class
x>
void
swap_args(x &a,x &b)
{
x
temp;
temp=a;
a=b;
b=temp;
}
void
main()
{
int
i=10,j=20;
double
x=10.1,y=23.3;
char
a='x',b='y';
clrscr();
cout<<"\n\n\t\t
*** TEMPLATE FUNCTION *** \n";
cout<<"\n
original
i,j:"<<i<<"\t"<<j<<"\n";
cout<<"\n
original
a,b:"<<a<<"\t"<<b<<"\n";
cout<<"\n
original
x,y:"<<x<<"\t"<<y<<"\n";
swap_args(i,j);
swap_args(x,y);
swap_args(a,b);
cout<<"\n
swapped
i,j:"<<i<<"\t"<<j<<"\n";
cout<<"\n
swapped
a,b:"<<a<<"\t"<<b<<"\n";
cout<<"\n
swapped
x,y:"<<x<<"\t"<<y<<"\n";
getch();
}
TYPE CONVERSION
#include<iostream.h>
#include<conio.h>
class
invent2;
class
invent1
{
int
code;
int
items;
float
value;
float
price;
public:
invent1(int
a,int b,int c)
{
code=a;
items=b;
price=c;
}
void putdata()
{
cout<<"code:"<<code<<"\n";
cout<<"items:"<<items<<"\n";
cout<<"value:"<<value<<"\n";
}
int getcode()
{
return code;
}
int getitems()
{
return items;
}
float getprice()
{
return price;
}
operator float()
{
return(items*price);
}
};
/*operator
invent1()
{
invent1
temp;
temp.code=code;
temp.value=price*items;
return
temp;
}
};*/
class
invent2
{
int
code;
float
value;
public:
invent2()
{
code=0;
value=0;
}
invent2(int
x,float y)
{
code=x;
value=y;
}
void putdata()
{
cout<<"code:"<<code<<"\n";
cout<<"value:"<<value<<"\n";
}
invent2(invent1
p)
{
code=p.getcode();
value=p.getitems()*p.getprice();
}
};
int
main()
{
clrscr();
invent1
s1(100,5,140.0);
invent2
d1;
float
total_value;
total_value=s1;
d1=s1;
cout<<"product
details_invent type"<<"\n";
s1.putdata();
cout<<"stock
value"<<"\n";
cout<<"value_"<<total_value<<"\n\n";
cout<<"product
details_invent2 type"<<"\n";
d1.putdata();
return
0;
}
INHERITANCE
#include<iostream.h>
#include<conio.h>
class
student
{
protected:
int roll_number;
public:
void
get_number(int a)
{
roll_number=a;
}
void
put_number(void)
{
cout<<"roll
no:"<<roll_number<<"\n";
}
};
class
test:public student
{
protected:
float part1,part2;
public:
void
get_mark(float x,float y)
{
part1=x;
part2=y;
}
void
put_mark(void)
{
cout<<"marksobtained:"<<"\n"<<"part1"<<part1<<"\n"<<"part2="<<part2;
}
};
class
sports
{
protected:
float score;
public:
void
get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports
wt:"<<score<<"\n";
}
};
class
result:public test,public sports
{
float
total;
public:
void
display(void);
};
void
result::display(void)
{
total=part1+part2+score;
put_number();
put_mark();
put_score();
cout<<"total
score:"<<total<<"\n";
}
int
main()
{
clrscr();
result
student_1;
student_1.get_number(12.34);
student_1.get_mark(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
getch();
return
0;
}
No comments:
Post a Comment