You are on page 1of 23

1A) Design an employee class for reading and displaying the employee information, the

getInfo() and displayInfo() methods will be used repectively. Where getInfo() will
be private method.

#include<iostream.h>
class​ employee{
char​ name[​30​];
int​ salary;
private​:
void​ ​getInfo​(){
cout​<<​"Enter Name & Salary:"​;
cin​>>name>>salary;
}
public​:
void​ ​displayInfo​(){
getInfo();
cout​<<​"Name:"​<<name<<​"\tSalary:"​<<salary;
}
};
void​ ​main​(){
employee e;
e.displayInfo();
}

Output :
Enter Name & Salary:xyz 1234
Name:xyz Salary:1234
1B) Design the class student containing getData() and displayData() as two of its
methods which will be used for reading and displaying the student information
respectively.Where getData() will be private method.

#include<iostream.h>
class​ student{
char​ name[​30​];
int​ marks;
private​:
void​ ​getData​(){
cout​<<​"Enter Name & Marks:"​;
cin​>>name>>marks;
}
public​:
void​ ​displayData​(){
getData();
cout​<<​"Name:"​<<name<<​"\tMarks:"​<<marks;
}
};
void​ ​main​(){
student s;
s.displayData();
}

Output :
Enter Name & Marks:xyz 1234
Name:xyz Marks:1234
1C) Design the class Demo which will contain the following methods: readNo(),
factorial() for calculating the factorial of a number, reverseNo() will reverse the
given number, isPalindrome() will check the given number is palindrome,
isArmstrong() which will calculate the given number is armStrong or not.Where
readNo() will be private method.

#include<iostream.h>
class​ Demo{
int​ n;
private​:
void​ ​readNo​(){
cout​<<​"\nEnter Number:"​;
cin​>>n;
}
public​:
void​ ​factorial​(){
readNo();
int​ f=​1​,i;
for​(i=​1​;i<=n;i++){
f*=i;
}
cout​<<​"Factorial :"​<<f;
}
void​ ​reverseNo​(){
readNo();
int​ d=​0​,r=​0​;
while​(n!=​0​){
d=n%​10​;
r=d+(r*​10​);
n=n/​10​;
}
cout​<<​"Reverse :"​<<r;
}
void​ ​isPalindrome​(){
readNo();
int​ x=n,d=​0​,r=​0​;
while​(n!=​0​){
d=n%​10​;
r=d+(r*​10​);
n=n/​10​;
}
cout​<<​"Palindrome :"​;
if​(x==r){
cout​<<​"true"​;
}
else​{
cout​<<​"false"​;
}
}
void​ ​isArmstrong​(){
readNo();
int​ x=n,d=​0​,a=​0​;
while​(n!=​0​){
d=n%​10​;
a=a+d*d*d;
n=n/​10​;
}
cout​<<​"Armstrong :"​;
if​(x==a){
cout​<<​"true"​;
}
else​{
cout​<<​"false"​;
}
}
};
void​ ​main​(){
Demo d;
d.factorial();
d.reverseNo();
d.isPalindrome();
d.isArmstrong();
}

Output :
Enter Number:5
Factorial :120
Enter Number:5
Reverse :5
Enter Number:5
Palindrome :true
Enter Number:5
Armstrong :false
1D) Write a program to demonstrate function definition outside class and accessing
class members in function definition.

#include<iostream.h>
class​ add{
int​ a,b;
public​:
void​ ​data​();
};
void​ add::data(){
cin​>>a>>b;
cout​<<​"Add :"​<<a+b;
}
void​ ​main​(){
add a;
a.data();
}

Output :
25
35
Add :60
2A) Write a friend function for adding the two complex numbers, using a single class.

#include<iostream.h>
class​ complex{
public​:
int​ r1,i1,r2,i2,ar,ai;
void​ ​getData​(){
cout​<<​"Enter r1 + i1:"​;
cin​>>r1>>i1;
cout​<<​"Enter r2 + i2:"​;
cin​>>r2>>i2;
}
friend​ ​void​ ​add​(​complex​);
};
void​ ​add​(​complex​ c1){
c1.ar=c1.r1+c1.r2;
c1.ai=c1.i1+c1.i2;
cout​<<​"Answer:"​<<c1.ar<<​" + "​<<c1.ai<<​"i"​;
}
void​ ​main​(){
complex​ c;
c.getData();
add(c);
}

Output :
Enter r1 + i1:25 35
Enter r2 + i2:35 25
Answer:60 + 60i
2B) Write a friend function for adding the two different distances and display its sum,
using two classes.

#include<iostream.h>
class​ dist2;
class​ dist1{
int​ in1,ft1;
public​:
void​ ​getData​(){
cout​<<​"Enter in & ft 1:"​;
cin​>>in1>>ft1;
}
friend​ ​void​ ​add​(dist1,dist2);
};
class​ dist2{
int​ in2,ft2;
public​:
void​ ​getData​(){
cout​<<​"Enter in & ft 2:"​;
cin​>>in2>>ft2;
}
friend​ ​void​ ​add​(dist1,dist2);
};
void​ ​add​(dist1 d1,dist2 d2){
int​ sumin,sumft;
sumin=d1.in1+d2.in2;
sumft=d1.ft1+d2.ft2;
cout​<<​"Sum = "​<<sumin<<​"In, "​<<sumft<<​"Ft."​;
}
void​ ​main​(){
dist1 dd1;
dist2 dd2;
dd1.getData();
dd2.getData();
add(dd1,dd2);
}

Output :
Enter in & ft 1:25 35
Enter in & ft 2:25 35
Sum = 50In, 70Ft.
2C) Write a friend function for adding the two matrix from two different classes and
display its sum.

#include<iostream.h>
class​ mat2;
class​ mat1{
int​ a1[​3​][​3​];
public​:
void​ ​getData​(){
cout​<<​"Enter Matrix 1:"​;
for​(​int​ i=​0​;i<​3​;i++){
for​(​int​ j=​0​;j<​3​;j++){
cin​>>a1[i][j];
}
}
}
friend​ ​void​ ​add​(mat1,mat2);
};
class​ mat2{
int​ a2[​3​][​3​];
public​:
void​ ​getData​(){
cout​<<​"Enter Matrix 2:"​;
for​(​int​ i=​0​;i<​3​;i++){
for​(​int​ j=​0​;j<​3​;j++){
cin​>>a2[i][j];
}
}
}
friend​ ​void​ ​add​(mat1,mat2);
};
void​ ​add​(mat1 m1,mat2 m2){
int​ sum[​3​][​3​];
for​(​int​ i=​0​;i<​3​;i++){
for​(​int​ j=​0​;j<​3​;j++){
sum[i][j]=m1.a1[i][j]+m2.a2[i][j];
cout​<<sum[i][j]<<​"\t"​;
}
cout​<<​"\n"​;
}
}
void​ ​main​(){
mat1 mm1;
mat2 mm2;
mm1.getData();
mm2.getData();
add(mm1,mm2);
}

Output :
Enter Matrix 1:1 2 3 4 5 6 7 8 9
Enter Matrix 2:9 8 7 6 5 4 3 2 1
10 10 10
10 10 10
10 10 10
3A) Design a class Complex for adding the two complex numbers and also show the
use of constructor.

#include<iostream.h>
class​ complex{
int​ r1,r2,i1,i2,ar,ai;
public​:
complex​(){
cout​<<​"Enter R1+I1 & R2+I2 :"​;
cin​>>r1>>i1>>r2>>i2;
}
void​ ​add​(){
ar=r1+r2;
ai=i1+i2;
cout​<<​"Sum : "​<<ar<<​" + "​<<ai<<​"i"​;
}
};
void​ ​main​(){
complex​ c;
c.add();
}

Output :
Enter R1+I1 & R2+I2 :25 35
25 35
Sum : 50 + 70i
3B) Design a class Geometry containing the methods area() and volume() and also
overload the area() function .

#include<iostream.h>
class​ geometry{
int​ l=​5​,b=​5​,h=​5​;
public​:
void​ ​area​(​int​ a, ​int​ b){
int​ ans;
ans=a*b;
cout​<<​"\nRectangle : "​<<ans;
}
void​ ​area​(​float​ r){
float​ ans;
ans=​3.1415​*r*r;
cout​<<​"\nCircle : "​<<ans;
}
void​ ​volume​(){
int​ ans;
ans=l*b*h;
cout​<<​"\nCube : "​<<ans;
}
};
void​ ​main​(){
geometry g;
g.area(​5​);
g.area(​5​,​4​);
g.volume();
}

Output :
Circle : 78.5375
Rectangle : 20
Cube : 125
3C) Design a class StaticDemo to show the implementation of static variable and static
function.

#include<iostream.h>
class​ staticDemo{
public​:
static​ ​int​ a;
static​ ​void​ ​stat​(){
staticDemo::a=​10​;
cout​<<​"A : "​<<staticDemo::a;
}
};
int​ staticDemo::a;
void​ ​main​(){
staticDemo::stat();
}

Output :
A : 10
4A) Overload the operator unary(-) for demonstrating operator overloading.

#include<iostream.h>
class​ demo{
public​:
operator​ -(){
cout​<<​"- operator will print this"​;
}
};
void​ ​main​(){
demo d;
-d;
}

Output :
- operator will print this
4B) Overload the operator + for adding the timings of two clocks, And also pass objects
as an argument.

#include<iostream.h>
class​ time{
public​:
int​ hr,mn,sc;
void​ ​read​(){
cout​<<​"Enter hh:mm ="​;
cin​>>hr>>mn;
}
time ​operator​ +(time t2){
time t3;
t3.mn=mn+t2.mn;
t3.hr=hr+t2.hr;
if​(t3.mn>​60​){
t2.hr+=​1​;
t3.mn-=​60​;
}
return​ t3;
}
void​ ​disp​(){
cout​<<​"Time : "​<<hr<<​":"​<<mn;
}
};
void​ ​main​(){
time c1,c2,c3;
c1.read();
c2.read();
c3=c1+c2;
c3.disp();
}

Output :
Enter hh:mm =25 10
Enter hh:mm =20 35
Time : 45:45
4C) Overload the + for concatenating the two strings. For e.g “Py” + “thon” = Python

#include<iostream.h>
#include<string.h>
class​ strings{
public​:
char​ str[​20​];
void​ ​getData​(){
cout​<<​"Enter String : "​;
cin​>>str;
}
strings ​operator​ +(strings x){
strings s;
strcat​(str,x.str);
strcpy​(s.str,str);
cout​<<s.str;
}
};
void​ ​main​(){
strings s1,s2,s3;
s1.getData();
s2.getData();
s3=s1+s2;
}

Output :
Enter String : Py
Enter String : thon
Python
5a) Single level
#include<iostream>
using namespace std;
class AAA{
public:
int a=5;
};
class BBB:private AAA{
public:
void disp(){
cout<<a;
}
};
main(){
BBB bb;
bb.disp();
}
Output:
5
5b) Multiple Inheritance
#include<iostream>
using namespace std;
class AAA{
public:
int a=5;
};
class BBB{
public:
int b=7;
};
class CCC:private AAA,private BBB{
public:
void disp(){
cout<<a<<b;
}
};
main(){
CCC cc;
cc.disp();
}
Output:
57
5c) Hierarfchical Inheritance
#include<iostream>
using namespace std;
class AAA{
public:
int a=5;
};
class BBB:private AAA{
public:
void disp(){
cout<<a;
}
};
class CCC:private AAA{
public:
void disp(){
cout<<a;
}
};
main(){
CCC cc;
BBB bb;
cc.disp();
bb.disp();
}
Output:
55
6a) Method Overriding
#include<iostream>
using namespace std;
class par{
public:
virtual void fun(){
cout<<"parent";
}
};
class child:public par{
public:
void fun(){
cout<<"Child";
}
};
main(){
child c;
par &p=c;
p.fun();
c.fun();
}
Output:
ChildChild
6b) Virtual Function
#include<iostream>
using namespace std;
class par{
public:
virtual void fun(){
cout<<"parent";
}
};
class child:public par{
public:
void fun(){
cout<<"Child";
}
};
main(){
child c;
par &p=c;
p.fun();
c.fun();
}
Output:
ChildChild
6c) Abstract Class
#include<iostream>
using namespace std;
class par{
public:
virtual void fun()=0;
};
class child:public par{
public:
void fun(){
cout<<"Child";
}
};
main(){
child c;
par &p=c;
p.fun();
c.fun();
}
Output:
ChildChild
7a) String Operation
#include<iostream>
#include<string.h>
using namespace std;
main(){
char a[10],b[10],c[10];
cin>>a;
cin>>b;
cout<<strlen(a)<<endl;
cout<<strcat(a,b)<<endl;
cout<<strrev(a)<<endl;
cout<<strcpy(c,a)<<endl;
cout<<strcmp(a,c)<<endl;
}
Output:
xyz
123
3
xyz123
321zyx
321zyx
0

You might also like