Vous êtes sur la page 1sur 22

1.

Write

a program that shows the property of data abstraction.


#include <iostream>
using namespace std;
class Adder
{
public:

// constructor

Adder(int i = 0)
{
total = i;
}

// interface to outside world


void addNum(int number)
{
total += number;
}
// interface to outside world
int getTotal()
{
return total;
};
private:
int total;
};
int main( )

// hidden data from outside world

{
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);

cout << "Total " << a.getTotal() <<endl;


return 0;

When the above code is compiled and executed, it produces the following result:
Output :
Total 60

2.Write a program to calculate the area of circle, rectangle


and triangle using function overloading.
#include<iostream.h>
#include<conio.h>
const float pi=3.14;
float area(float n,float b,float h)
{
float ar;
ar=n*b*h;
return ar;
}
float area(float r)
{
float ar;
ar=pi*r*r;
return ar;
}
float area(float l,float b)
{
float ar;
ar=l*b;

return ar;
}
void main()
{
float b,h,r,l;
float result;
clrscr();
cout<<\nEnter the Base & Hieght of Triangle: \n;
cin>>b>>h;
result=area(0.5,b,h);
cout<<\nArea of Triangle: <<result<<endl;
cout<<\nEnter the Radius of Circle: \n;
cin>>r;
result=area(r);
cout<<\nArea of Circle: <<result<<endl;
cout<<\nEnter the Length & Bredth of Rectangle: \n;
cin>>l>>b;
result=area(l,b);
cout<<\nArea of Rectangle: <<result<<endl;
getch();
}

Output
Enter the Base & Hieght of Triangle:
10
15

Area of Triangle: 75
Enter the radius of circle:
1
Area of Circle: 3.14

Enter the length and breadth of rectangle:


4
5
Area of rectangle is 20

4.Write a program to calculate the total marks of a student


using the concept of virtual base class
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};

class test:virtual public student


{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports:public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{

cout<<"\n\tSports Score is:"<<score;


}
};
class result:public test,public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}

Output:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80

Enter Sports Score: 80


Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250

5.Write a program to declare friend function in two classes. Calculate the


sum of integers of both the classes using friend sum() function
#include<iostream.h>
#include<conio.h>
class A
{
int val1,val2,val3;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend sum(val1,val2);
};
class b
{
int val1,val2,val3;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend sum(val1,val2);
};
void SUM(Val1,val2)
{

Val3=val1+val2;
}
void main()
{
clrscr();
a obj;
obj.get();
cout<<"\n sum value is : "<<sum(obj);
getch();
}
Output:
Enter two values: 10, 20
Sum Value is: 30

7.Write a 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<<"Enter 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:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}

Output:
Enter the value of Complex Numbers a, b
4
5
Enter the value of Complex Numbers a, b
2
2
Input Values
4 + 5i
2 + 2i

Result
6 + 7i
2 + 3i

8.Write a profram to Explain the concept of Overloading Input


and Output operator with the help of program
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
// 0 to infinite
int inches;
// 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
friend ostream &operator<<( ostream &output,
const Distance &D )
{
output << "F : " << D.feet << " I : " << D.inches;

return output;
}
friend istream &operator>>( istream &input, Distance &D )
{
input >> D.feet >> D.inches;
return input;
}
};
int main()
{
Distance D1(11, 10), D2(5, 11), D3;
cout << "Enter the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance :" << D2 << endl;
cout << "Third Distance :" << D3 << endl;

return 0;
}
When the above code is compiled and executed, it produces the following result:

Output:
Enter the value of object :
70
10
First Distance : F : 11 I : 10
Second Distance :F : 5 I : 11
Third Distance :F : 70 I : 10

9. Write a program to declare default arguments in constructor


and obtain the power of the number
#include<iostream>
#include<conio.h>
using namespace std;
class Example

{
// Variable Declaration

int a,b;
public:

Example()

//Constructor
{
// Assign Values In Constructor

a=5;
b=2;
cout<<"Im Constructor\n";
}
void Display_pow(int a,int b)
{

r=pow(a,b);
cout<<"power of number is :"<<r<<"\t";
}
};

int main()

Example Object;
// Constructor invoked.
Object.Display_pow();
// Wait For Output Screen
getch();
return 0;
}

Output:Power of number is:


25

11.Write a program to find out the student details using multiple inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
// sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;

}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}

Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66

: "<<tot;

12. Write a program of Virtual Class in Hybrid Inheritance.


#include<iostream.h>
#include<conio.h>
class stu
{
protected: int rno;
public: void get_no(int a)
{
rno=a;
}
void put_no(void)
{
c out<<"Roll no"<<rno<<"\n";
}
};
class test:public stu
{
protected: float part1,part2;
public: void get_mark(float x,float y)
{
part1=x; part2=y;

}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected: float score; public: void getscore(float s)
{
score=s;
}
void putscore(void) { cout<<"sports:"<<score<<"\n";
}
};
class result: public test, public sports
{
float total;
public: void display(void);
};
void result::display(void)
{
total=part1+part2+score; put_no(); put_marks(); putscore();
cout<<"Total Score="<<total<<"\n";
}
int main()
{
clrscr();
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();

return 0 ;
}

OUTPUT
Roll no 123
Marks obtained : part1=27.5
Part2=33
Sports=6
Total score = 66.5

13.Write a program to display the elements of integer and float


array using template variables with member function.
#include<iostream.h>

#include<conio.h>
template<class t>
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<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

14.Write a program to show the concept of virtual function.


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
};
class drive:public base
{
public:
void display()
{

cout<<"\n Drive class display:";


}
void show()
{
cout<<"\n Drive class show:";
}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}

Output:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show

15. Write a program to perform exception handling with multiple catch.


#include<iostream.h>
#include<conio.h>
void test(int x)
{
try
{
if(x>0)
throw x;
else
throw 'x';
}
catch(int x)
{
cout<<"Catch a integer and that integer is:"<<x;
}
catch(char x)
{
cout<<"Catch a character and that character is:"<<x;

}
}
void main()
{
clrscr();
cout<<"Testing multiple catches\n:";
test(10);
test(0);
getch();
}

Output:
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x

http://www.codeproject.com/Tips/596709/Implementation-of-Type-of-Association

http://www.dailyfreecode.com/mysearchresult.aspx?q=student+details+using+multile
vel+inheritance&stype=all

Vous aimerez peut-être aussi