C++ Unit 2 Codes
C++ Unit 2 Codes
for
1) Teacher code, name, subject and publication
2) O cer code, name and grade
3) Typist code, name, speed and daily wages and displays it using hierarchal
inheritance
#include <iostream>
class sta
protected:
int code;
char name[20];
public:
cin>>code;
cin>>name;
cout<<"\nNAME :-"<<name;
cout<<"\nCODE :-"<<code;
ffi
ff
ff
ff
ff
}
};
char sub[20];
char pub[20];
public:
void create(void)
getsta ();
cin>>sub;
cin>>pub;
void display(void)
dispsta ();
cout<<"\nSUBJECT :-"<<sub;
cout<<"\nPUBLICATION:-"<<pub;
};
ffi
ff
ff
ff
ff
char grade;
public:
void create(void)
getsta ();
cin>>grade;
void display(void)
dispsta ();
cout<<"\nGRADE :-"<<grade;
};
oat speed;
public:
void gettypist(void)
getsta ();
cin>>speed;
fl
ff
ff
ff
ff
void disptypist(void)
dispsta ();
cout<<"\nSPEED :-"<<speed;
};
oat dailywages;
public:
void create(void)
gettypist();
cin>>dailywages;
void display(void)
disptypist();
cout<<"\nDAILY WAGES:-"<<dailywages;
};
int main()
fl
ff
teacher o1t[10];
casual o1c[10];
o cer o1o[10];
int choice,i;
char test;
while(1)
int count;
start:
cout<<"1) Teachers\n";
cout<<"2) O cer\n";
cout<<"3) Typist\n";
cout<<"4) Exit\n";
cin>>choice;
switch(choice)
case 1 : while(1)
cout<<"\n=====TEACHERS INFORMATION=====\n\n";
ffi
ffi
cout<<"1) Create\n";
cout<<"2) Display\n";
cin>>choice;
switch(choice)
case 1 : for(count=0,i=0;i<10;i++)
cout<<endl;
o1t[i].create();
count++;
cout<<endl;
cout<<"Enter y or n:-";
cin>>test;
if(test=='y' || test=='Y')
continue;
out1:
break;
case 2 : for(i=0;i<count;i++)
cout<<endl;
o1t[i].display();
cout<<endl;
break;
case 2 : while(1)
cout<<"\n=====OFFICERS INFORMATION=====\n\n";
cout<<"1) Create\n";
cout<<"2) Display\n";
cin>>choice;
switch(choice)
case 1 : for(count=0,i=0;i<10;i++)
cout<<endl;
o1o[i].create();
count++;
cout<<endl;
cout<<"Enter y or n:-";
cin>>test;
if(test=='y' || test=='Y')
continue;
out2:
break;
case 2 : for(i=0;i<count;i++)
cout<<endl;
o1o[i].display();
cout<<endl;
break;
case 3 : while(1)
cout<<"\n=====TYPIST INFORMATION=====\n\n";
cout<<"1) Create\n";
cout<<"2) Display\n";
cin>>choice;
switch(choice)
case 1 : for(count=0,i=0;i<10;i++)
cout<<endl;
o1c[i].create();
count++;
cout<<endl;
cout<<"Enter y or n:-";
cin>>test;
if(test=='y' || test=='Y')
continue;
out3:
break;
case 2 : for(i=0;i<count;i++)
cout<<endl;
o1c[i].display();
cout<<endl;
break;
end:
return 0;
#include<iostream>
class student {
int rno;
public:
void getnumber() {
cin>>rno;
void putnumber() {
};
public:
void getmarks() {
cin>>SUB1;
cin>>SUB2;
void putmarks() {
};
ff
fi
public:
int score;
void getscore() {
cin>>score;
void putscore() {
};
int total;
public:
void display() {
putnumber();
putmarks();
putscore();
};
int main()
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0;
Aim: - WAP to illustrate 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. Pass four variable to gamma class
object which will further send one integer variable to alpha(),one oat type
variable to beta().Show the order of execution by invoking constructor of
derived class.
#include<iostream>
fl
class alpha
int x;
public:
alpha(int i)
x=i;
cout<<"alpha initialized\n";
void show_x()
cout<<"x="<<x<<"\n";
};
class beta
oat y;
public:
beta( oat j)
y=j;
cout<<"beta initialized\n";
void show_y()
fl
fl
{
cout<<"y="<<y<<"\n";
};
int m,n;
public:
m=c,n=d;
cout<<"gamma initialized\n";
void show_mn()
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
};
int main()
gamma g(10,12.75,35,50);
g.show_x();
fl
g.show_y();
g.show_mn();
return 0;
AIM: -WAP to calculate and display cube of an integer and oat variable using
function overloading.
#include<iostream>
int main()
int x;
oat y;
int cube(int);
cin>>x;
cin>>y;
return 0;
return num*num*num;
return num*num*num;
#include <iostream>
fl
fl
fl
fl
fl
fi
fi
fi
fi
fl
class Integer
private:
int i;
public:
Integer(int i = 0)
this->i = i;
Integer& operator++()
++i;
return *this;
void display()
};
int main()
Integer i1(3);
i1.display();
Integer i2 = ++i1;
i1.display();
i2.display();
Aim:-WAP for creating a matrix class which can handle integer matrices of
di erent dimensions. Overload the operator (+) for addition and (==)
comparison of matrices.
#include<iostream>
class matrix
private:long m[5][5];
public:void getdata();
ff
matrix operator+(matrix);
matrix operator-(matrix);
};
return 1;
return 0;
void matrix::getdata()
cin>>row;
cin>>col;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>m[i][j];
matrix temp;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
temp.m[i][j]=m[i][j]+am.m[i][j];
temp.row=row;
temp.col=col;
return temp;
matrix temp;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
temp.m[i][j]=m[i][j]-sm.m[i][j];
temp.row=row;
temp.col=col;
return temp;
for(int i=0;i<d.col;i++)
for(int j=0;j<d.col;j++)
fout<<d.m[i][j];
cout<<" ";
cout<<endl;
return fout;
int main()
matrix m1,m2,m3,m4;
m1.getdata();
m2.getdata();
if(m1==m2)
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<m3;
cout<<"subtraction of matrices\n";
cout<<m4;
else
Aim:- WAP to create a class Pairs. Objects of type Pairs can be used in any
situation where ordered pairs are needed. Our Task is to overload operator >>
and << so that objects of class Pairs are to be input and output in the form
(5,3) (5,-6) (-5,6) or (-5,-3).There is no need to implement any constructor/
method .
#include <iostream>
#include <cstring>
class Pairs {
private:
char numpair[20];
public:
output<<p.numpair;
return output;
}
char pair[20];
input>> pair;
intlen = strlen(pair);
// as per given format "(x,y)" the min length of string should be 5 and also
// the string should contain the rst character "(", the last character ")"
// in case of Invalid value assing blank string to the pair value
strcpy(p.numpair, "");
}
else {
// in case of valid value copy the input value to the object member datq
strcpy(p.numpair, pair);
}
return input;
}
};
int main() {
Pairs p;
cin>> p;
return 0;
AIM: - WAP to create a class that will maintain the records of person with
details Name and Age) and nd the eldest among them. The program must
fi
fi
use this pointer to return the result by overloading> operator among two
objects.
//AIM: - WAP to create a class that will maintain the person of person with details
Name and Age) and nd the eldest among them. The program must use this
pointer to return the result by overloading> operator among two objects.
#include <iostream>
class person
int age;
string name;
public:
person(){};
void show()
cout<<name<<" : "<<age<<endl;
person eldest(person p)
};
int main()
person
PER[4]={person("ANIL",11),person("MEHAK",35),person("AJAY",30),person("OJAS
",19)};
person p;
for(int i=0;i<2;i++)
p = PER[i].eldest(PER[i+1]);
p.show();
return 0;
class Number
private:
int num;
public:
//constructor
Number(){ num=0; };
fi
//member function to get input
{
cin>>num;
}
void displayNumber()
{
cout<<"Num: "<<num<<endl;
}
};
//Main function
int main()
Number N;
N.inputNumber();
N.displayNumber();
Number *ptrN;
ptrN->displayNumber();
ptrN->inputNumber();
ptrN->displayNumber();
return 0;
#include<string.h>
class media
protected:
char title[50];
oat price;
public:
fl
media(char *s, oat a)
};
pages = p;
void display();
};
time =t;
void display();
};
cout<<"\n Title:"<<title;
cout<<"\n Title:"<<title;
int main()
int pages;
cout<<"\n Title:";
cin>>title;
cout<<"\n Price:";
cin>>price;
cin>>time;
fl
fl
fl
fl
fl
fl
tape tape1(title, price, time);
media* list[2];
list[0] = &book1;
cout<<"\n..............Book. ";
list[0]->display ();
cout<<"\n..............Tape. ";
list[1]->display ();
return 0;