1. Write
a Program to
design a class
having static member
function named showcount() which
has the property
of displaying
the number of
objects
created
of the class.
#include<iostream.h>
#include<conio.h>
class test
|
{
|
int code;
static int
count;
|
public:
void
setcode(void)
|
{
|
code = ++count;
|
}
void showcode(void)
|
{
|
cout<<"object
number:"<<code<<"\n";
|
}
static void showcount(void)
|
{
|
cout<<"count:"<<count<<"\n";
|
}
};
|
int test :: count;
int main()
|
{
|
test t1,t2;
t1.setcode();
t2.setcode();
test ::
showcount();
test t3;
t3.setcode();
test ::
showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
|
}
2.Write a Program which
creates & uses array of object of a class.( for eg.
implementing
the list of Managers of a Company having details such as Name,
Age,
etc..).
#include<iostream.h>
#include<conio.h>
class
employee
|
{
|
char name [30];
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:"<<name<<"\n";
cout<<"Age: "<<age<<"\n";
|
}
|
const int size=3;
int main()
{
employee
manager[size];
for(int
i=0; i<size; i++)
|
{
|
cout<<"\n Details of
manager"<<i+1<<"\n";
manager[i].getdata();
|
}
|
cout<<"\n";
for(i=0;
i<size; i++)
|
{
|
cout<<"\n
Manager"<<i+1<<"\n";
manager[i].putdata();
|
}
|
return 0;
|
}
3.Write a Program to find
Maximum out of Two Numbers using friend function.
Note:
Here one number is a member of one class and the other number is member
of
some other class.
#include<iostream.h>
#include<conio.h>
class ABC;
class XYZ
|
{
|
int x;
|
public:
void
setvalue(int i)
|
{
|
x=i;
|
}
friend void max(XYZ, ABC);
|
};
|
class ABC
|
{
|
int a;
|
public:
void
setvalue(int i)
|
{
|
a=i;
|
}
friend void max(XYZ, ABC);
|
};
|
void max (XYZ m, ABC n)
|
{
|
if(m.x>=n.a)
|
else
}
int main()
|
{
|
cout<<m.x;
cout<<n.a;
|
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
return 0;
|
}
4. Write a Program using
copy constructor to copy data of an object to another
object.
#include<iostream.h>
#include<conio.h>
class code
|
{
|
int id;
|
public:
code(){}
code(int a)
|
{
}
|
id = a;
|
code(code & x)
|
{
}
|
id = x.id;
|
void display(void)
|
{
}
|
cout<<id;
|
};
|
int main()
{
code A(100);
code B(A);
code C = A;
code D;
D = A;
|
cout<<"\n id of A:";
A.display();
cout<<"\n
id of B:";
B.display();
cout<<"\n
id of C:";
C.display();
cout<<"\n
id of D:";
D.display();
return 0;
|
}
5.Write a Program to allocate
memory dynamically for an objects of a given class
using class's constructor.
#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(void)
|
{
}
|
cout<<name<<"\n";
|
void join(String &a,
String &b);
|
};
|
void String :: 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);
|
};
|
int main()
{
char *first
= "Joseph";
String
name1(first), name2("Louis "), name3("Lagrange"),s1,s2;
s1.join(name1,
name2);
s2.join(s1,
name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
|
return 0;
|
}
|
6.Write a program to design a
class representing complex numbers and having the
functionality of performing addition & multiplication of two complex numbers
using operator overloading.
#include <iostream.h>
class
complex
{
private:
float real,
|
imag;
|
public:
|
complex( )
{
}
complex(
float r, float i )
|
{
|
real = r;
imag = i;
|
}
|
void getdata( )
|
{
|
float r,
i;
cout
<< endl << "Enter real and imaginary part ";
cin
>> r >> i;
real = r;
imag = i;
|
}
|
void setdata( )
|
{
|
real = r;
imag = i;
|
}
|
void displaydata( )
|
{
|
cout << endl <<
"real = " << real;
cout<<endl<<Imaginary
= "<<imag;
|
}
|
complex operator +( complex c )
|
{
|
complex
t;
t.real = real
+ c.real;
t.imag = imag
+ c.imag;
|
}
|
complex operator *( complex c )
|
{
|
complex t;
t.real =
real * c.real - imag * c.imag;
t.imag =
real * c.imag + c.real * imag;
return t;
|
}
|
}
;
|
void main( )
{
|
complex
c1,
c2 ( 1.2,
-2.5 ),
c3,
c4;
c1.setdata(
2.0, 2.0 );
c3 = c1 + c2;
c3.displaydata(
);
c4.getdata(
);
complex c5 ( 2.5, 3.0 ),
c6;
c6 = c4 * c5;
c6.displaydata(
);
complex c7;
c7 = c1 + c2
* c3;
c7.displaydata(
);
|
}
7.Write a Program to overload
operators like *, <<, >> using friend function.
The following overloaded operators should work for a class vector.
#include<iostream.h>
#include<conio.h>
const size =
3;
class vector
|
{
|
int v[size];
|
public:
vector();
vector(int
*x);
friend
vector operator *(int a, vector b);
friend
vector operator *(vector b, int a);
friend
istream & operator >>(istream &, vector &);
|
friend ostream & operator
<<(ostream &, vector &);
|
};
|
vector ::vector()
|
{
|
for(int i=0;i<size;i++)
v[i]=0;
|
}
|
vector :: vector(int *x)
|
{
|
for(int i=0; i<size; i++)
v[i] = x[i];
|
}
|
vector operator *(int a, vector b)
|
{
|
vector c;
for(int i=0;
i<size; i++)
c.v[i] = a *
b.v[i];
return c;
|
}
|
vector operator *(vector b,
int a)
|
{
|
vector c;
for(int
i=0; i<size; i++)
c.v[i] =
b.v[i] * a;
return c;
|
}
|
istream & operator
>> (istream &din, vector &b)
|
{
|
for(int i=0; i<size;
i++)
din>>b.v[i];
return(din);
|
}
|
ostream & operator << (ostream
&dout, vector &b)
{
|
8.Write a Program
illustrating how the constructors are implemented and the order in
which they are called when the classes are inherited. Use three classes
named alpha, beta, gamma such that alpha,beta are base class and gamma
is derived class inheriting alpha & beta
#include<iostream.h>
#include<conio.h>
class alpha
|
{
|
int x;
|
public:
alpha(int i)
|
{
|
x=i;
|
cout<<"alpha initialized\n";
|
}
|
void show_x(void)
|
{
}
|
cout<<"x="<<x<<"\n";
|
};
|
class beta
|
{
|
float y;
|
public:
beta(float
j)
|
{
|
y=j;
cout<<"beta
initialized\n";
|
}
|
void show_y(void)
|
{
}
|
cout<<"y=
"<<y<<"\n";
|
};
|
class gamma : public beta, public alpha
|
{
|
int m,n;
|
public:
gamma(int
a, float b, int c, int d):
alpha(a),
beta(b)
|
{
|
m
|
n = d;
|
cout<<"gamma
initialized\n";
|
}
|
void show_mn(void){
cout<<"m="<<m<<"\n";
|
cout<<"n="<<n<<"\n";
|
}
|
};
|
void main()
|
{
|
gamma g(5, 10.75, 20, 30);
g.show_x();
g.show_y();
g.show_mn();
|
}
|
9.Write a Program to
illustrate the use of pointers to objects whch are
related
by inheritance.
#include<iostream.h>
class BC
{
public:
int b;
void show()
|
{
cout<<"b="<<b<<"\n";;
}
|
};
|
class DC : public BC
|
{
|
public:
int d;
void show()
|
{
|
cout<<"b="<<b<<"\n"
<<"d="<<d<<"\n";
|
}
|
};
int main()
|
{
|
BC *bptr;
BC base;
bptr =
&base;
bptr->b
= 100;
cout<<"bptr
points to base object\n";
bptr->show
();
|
DC derived;
bptr =
&derived;
bptr->b
= 200;
cout<<"bptr
now points to derived object\n";
bptr->show
();
DC *dptr;
dptr =
&derived;
dptr->d
= 300;
cout<<"dptr
is derived type pointer\n";
dptr->show
();
cout<<"Using
((DC *)bptr)\n";
((DC
*)bptr)->d = 400;
((DC
*)bptr)->show ();
return 0;
|
}
10.Write a program
illustrating the use of virtual functions in class.
#include<iostream.h>
class Base
|
{
|
public:
void
display()
|
{
|
cout<<"\n
Display Base";
}
|
virtual void show()
|
{
}
|
cout<<"\n Show Base:";
|
};
|
class Derived : public Base
|
{
|
public:
void
display()
|
{
}
|
cout<<"\n Display Derived";
|
void show()
|
{
}
|
cout<<"\n Show
Derived";
|
};
int main()
|
{
|
Base B;
Derived D;
Base *bptr;
cout<<"\n
bptr points to Base\n";
bptr =
&B;
bptr
->display ();
bptr
->show ();
cout<<"\n\n
bptr points to derived\n";
bptr =
&D;
bptr
->display ();
bptr
->show ();
return 0;
|
}
11.Write a program
illustrating the use of virtual functions in class.
#include<iostream.h>
#include<conio.h>
class Base
|
{
|
public:
void
display()
|
{
|
cout<<"\n
Display Base";
|
}
virtual void show()
|
{
|
cout<<"\n Show Base:";
|
}
};
|
class Derived : public Base
|
{
|
public:
void
display()
|
{
|
cout<<"\n Display Derived";
|
}
void show()
|
{
|
cout<<"\n Show
Derived";
|
}
};
int main()
|
{
|
Base B;
Derived D;
Base *bptr;
cout<<"\n
bptr points to Base\n";
bptr =
&B;
bptr
->display ();
bptr
->show ();
cout<<"\n\n
bptr points to derived\n";
bptr =
&D;
bptr
->display ();
bptr
->show ();
return 0;
|
}
12. Write a program to design
a class representing the information regarding digital
library (books, tape: book & tape should be separate classes having the base
class as media ). The class should have the
functionality for adding
new item, issuing, deposit etc. the program should use the runtime
polymorphism.
#include<iostream.h>
#include<string.h>
class media
|
{
|
protected:
char
title[50];
float
price;
public:
media(char
*s, float a)
|
{
|
strcpy(title, s);
price = a;
|
}
|
virtual void display(){}
|
};
|
class book : public media
|
{
|
int pages;
public:
book(char
*s, float a, int p) : media(s,a)
|
{
}
|
pages = p;
|
void display();
|
};
|
class tape : public media
|
{
|
float time;
public:
tape(char *
s, float a, float t):media(s,a)
|
{
}
|
time =t;
|
void display();
|
};
|
void book ::display()
|
{
|
cout<<"\n
Title:"<<title;
cout<<"\n
Pages:"<<pages;
cout<<"\n
Price:"<<price;
|
}
|
void tape ::display ()
|
{
|
cout<<"\n
Title:"<<title;
cout<<"\n
Play Time:"<<time<<"mins";
cout<<"\n
Price:"<<price;
|
}
|
int
main()
{
|
char * title = new char[30];
float price,
time;
int pages;
cout<<"\n
Enter Book Details \n";
cout<<"\n
Title:";
cin>>title;
cout<<"\n
Price:";
cin>>price;
cout<<"\n
Pages:";
cin>>pages;
book
book1(title, price, pages);
cout<<"\n
Enter Tape Details";
cout<<"\n
Title:";
cin>>title;
cout<<"\n
Price:";
cin>>price;
cout<<"\n
Play Times(mins):";
cin>>time;
tape
tape1(title, price, time);
media*
list[2];
list[0] =
&book1;
list[1] =
&tape1;
cout<<"\n
Media Details";
cout<<"\n..............Book.....";
list[0]->display
();
cout<<"\n..............Tape.....";
list[1]->display
();
return 0;
|
}
13.Write a program
implementing basic operation of class ios i.e.
setf,unsetf,precision
etc.
|
#include
#include
void main( )
|
{
|
int
|
i
|
<iostream.h>
<conio.h>
|
= 52;
|
float a = 425.0;
float
b = 123.500328;
char str[ ]
= "Dream. Then make it happend!";
clrscr( );
cout.setf(
ios::unitbuf );
cout.setf(
ios::stdio );
cout.setf(
ios::showpos );
cout
<< i << endl;
cout.setf(
ios::showbase );
cout.setf(
ios::uppercase );
cout.setf(
ios::hex, ios::basefield );
cout
<< i << endl;
cout.setf(
ios::oct, ios::basefield );
cout
<< i << endl;
cout.fill(
'0' );
cout
<< "Fill character " << cout.fill( ) << endl;
cout.setf(
ios::dec, ios::basefield );
cout.width(
10 );
cout
<< i << endl;
cout
<< setf( ios::left, ios::adjustfield );
cout.width(
10 );
cout
<< i << endl;
cout.setf(
ios::internal, ios::adjustfield );
cout.width(
10 );
cout
<< endl;
cout
<< endl;
cout.width(
10 );
cout
<< str << endl;
cout.width(
40 );
cout.setf(
ios::left, ios::adjustfield );
cout.width(
40 );
cout
<< str << endl;
cout.precision(
6 );
cout
<< "Precision" << cout.precision( );
cout.setf(
ios::showpoint );
cout.unsetf(
ios::showpos );
}
}
|
No comments:
Post a Comment