Sei sulla pagina 1di 41

1. First program in C++.

Source code
#include<iostream.h>

#include<conio.h>

int main()

cout<<"Every age has a language of its own.";

getch ();

return 0;

Output

1
2. Write a program to take three integers from user and display.

Source code
#include<iostream.h>

#include<conio.h>

int main()

int a,b,c;

cout<<"Enter three integers.\n";

cin>>a>>b>>c;

cout<<"The entered data are::\n"<<a<<endl<<b<<endl<<c;

getch ();

return 0;

Output

2
3. Inline Function.

Source code

#include <iostream.h>
#include <conio.h>

inline void area( int l=3, int b=5)


{
cout<<"Area="<<l*b;
}
int main()
{
area();
getch();
return 0;
}

Output

3
4. Display following:
1

11

111

1111

Source code
#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

int main()

cout<<setw(4)<<1<<endl

<<setw(4)<<11<<endl

<<setw(4)<<111<<endl

<<setw(4)<<1111;

getch();

return 0;

Output

4
5. Default Arguments.
Source code
#include<iostream.h>

#include<conio.h>

void area(int length=4, int breadth=2)

cout<<"Area="<<length*breadth<<endl;

int main()

area();

area(8); //This is the way of calling function in different ways.

area(8,8);

getch();

return 0;

Output

5
6. Write program to calculate interest with 3 different functions with same
name.
Source code
#include<iostream.h>
#include<conio.h>
void interest(int p)
{
int t=2, r=5;
cout<<"Interest="<<p*t*r<<endl;
}
void interest(int p, int t)
{
int r=10;
cout<<"Interest="<<p*t*r<<endl;
}
void interest(int p, int t, int r)
{
cout<<"Interest="<<p*t*r<<endl;
}
int main()
{
interest(500);
interest(500,2);
interest(500,2,10);
getch();
return 0;
}

Output

6
7. Take record of 10 squares (length) and display their perimeter.

Source code
#include<iostream.h>
#include<conio.h>
class Square{ public: //Class is a single unit which binds data.
int l;
void input()
{

cin>>l;
}
void output()
{
cout<<"Perimeter="<<4*l<<endl;
}}R[10];
int main()
{
int i;
cout<<"Enter length of 10 squares::\n";
for(i=0; i<10; i++)
R[i].input();
for(i=0; i<10; i++)
R[i].output();
getch();
return 0;}
Output

7
8. Write program to add two distances (feet).
Source code
#include<iostream.h>
#include<conio.h>
class distance{
public:
int ft;
void input()
{
cout<<"Please enter feet."<<endl;
cin>>ft;
}
void add(distance d)
{
ft=ft+d.ft;
}
void output()
{
cout<<"The added result is"<<endl<<ft<<endl;
}
}d,e;
int main()
{
d.input();
e.input();
e.add(d);
e.output();
getch();
return 0;
}

Output

8
9. Write program to add two times.
Source code
#include<iostream.h>
#include<conio.h>
class time{
public:
int hr, min, sec;
void input()
{
cout<<"Enter hour, minute and seconds.\n";
cin>>hr>>min>>sec;
}
void output()
{
cout<<"The added time is::\n";
cout<<hr<<"hr\t"<<min<<"min\t"<<sec<<"sec\t"<<endl;
}
void add(time p)
{
hr=hr+p.hr;
min=min+p.min;
sec=sec+p.sec;
if(sec>=60)
{
sec=sec-60;
min++;
}
if (min>=60)
{
min=min-60;
hr++;
}

9
}}A,B;
int main()
{
A.input();
B.input();
A.add(B);
A.output();
getch();
return 0;
}
Output

10
10. Write program to take records of 5 times (hr, min, sec) and display them.
Use constructor to take values.
Source code
#include<iostream.h>
#include<conio.h>
class time{
public:
int hr, min, sec;
time()
{
cout<<"Enter hour, minute and seconds.\n";
cin>>hr>>min>>sec;
}
void output()
{
cout<<"The entered records of time are::\n";
cout<<hr<<"hr\t"<<min<<"min\t"<<sec<<"sec\t"<<endl;
}
}A[5];
int main()
{
int i;
for(i=0; i<5; i++)
A[i].output();
getch();
return 0;
}

11
Output

12
11. Illustration of Constructor.
Source code
#include <iostream.h>
#include <conio.h>
class Rectangle{
public:
int length, breadth;

Rectangle() //using constructor


{
cout<<"Enter length and breadth of rectangle.\n" ;
cin>>length>>breadth;
}
void output()
{
cout<<"Area="<<length*breadth;
}}A;
int main()
{
A.output();
getch ();
return 0;
}
Output

13
12. Calculate area of 3 rectangles. Initialize all these different
Rectangles using constructors. (Constructor overloading)
Source code
#include <iostream.h>
#include <conio.h>
class Rectangle{
public:
int length, breadth;

Rectangle()
{
length=5; breadth=4;
}
Rectangle(int l)
{
length=l; breadth=8;
}
Rectangle(int l, int b)
{
length=l; breadth=b;
}
void area()
{
cout<<"Area= "<<length*breadth<<endl;
}};
int main()
{
Rectangle A, B(8), C(5,3);
A.area();
B.area();
C.area();
getch ();

14
return 0;
}
Output

15
13. Illustration of Destructor.
Source code
#include <iostream.h>
#include <conio.h>
class Example{
public:
Example()
{
cout<<"Constructor"<<endl;
}
~Example()
{
cout<<"Destructor"<<endl;
}};
void function()
{
Example A,B;
}
int main()
{
function ();
function ();
getch();
return 0;
}
Output

16
14. Write program to calculate area of 4 rectangle (length, breadth) make data
private.

Source code
#include <iostream.h>
#include <conio.h>
class Rectangle{
private:
int l,b;
public:
Rectangle ()
{
cout<<"Enter length and breadth."<<endl;
cin>>l>>b;
}
void area()
{
cout<<"Area of Rectangle="<<l*b<<endl;
}}A[4];
int main()
{
int i;
for(i=0; i<4; i++)
A[i].area();
getch();
return 0;
}

17
Output

18
15. Write program to calculate volume of 10 room(l, b, h). Make data private.
Source code
#include <iostream.h>
#include <conio.h>
class Room{
private:
int l, b, h;
public:
void input()
{
cout<<"Enter dimension (length, breadth and height) of"
<<"1o room"<<endl;
cin>>l>>b>>h;
}
void output()
{
cout<<"Volume="<<l*b*h<<endl;
}
}A[10];
int main()
{
int i;
for(i=0; i<10; i++)
A[i].input();
for(i=0; i<10; i++)
A[i].output();
getch();
return 0;
}

Output
19
20
16. Single Inheritance.
Source code
#include<iostream.h>
#include<conio.h>
class Employee{
protected:
int salary;
char name[20];
};
class Manager : public Employee
{
public:
void input()
{
cout<<"Enter name and salary"<<endl;
cin>>name>>salary;
}
void output()
{
cout<<"Name="<<name<<endl
<<"Salary="<<salary<<endl;
}
}M;
int main()
{
M.input();
M.output();
getch();
return 0;
}

21
Output

22
17. Create class named person (name, age, height). Then derive class named
Employee (Company_name, salary). Include member function to do I/O in
Employee class. Write main function to create program.
Source code
#include <iostream.h>
#include <conio.h>
class Person{
protected:
char name[10];
int age;
float height;
};
class Employee : public Person
{
char company_name[20];
int salary;
public:
Employee()
{
cout<<"Enter name, age, height,"
<<" company_name and salary\n";
cin>>name>>age>>height>>company_name>>salary;
}
void output()
{
cout<<"The entered records are::\n"
<<"Name="<<name<<endl
<<"Age="<<age<<endl
<<"Height="<<height<<endl
<<"Company name="<<company_name<<endl
<<"Salary="<<salary;
}};

23
int main()
{
Employee E;
E.output();
getch();
return 0;
}
Output

24
18. Create class named food (name, price) then derive class fruit (color) from
Class food. Finally derive class apple from fruit. Include member functions
to do I/O and write main function to execute it.
Source code
#include <iostream.h>
#include <conio.h>
class food{
protected:
char name[20];
int price;
};
class fruit : public food
{
protected:
char color[10];
};
class apple : public fruit
{
public:
Apple()
{
cout<<"Enter name, price and color of fruit.\n";
cin>>name>>price>>color;
}
void output()
{
cout<<"Name="<<name<<endl
<<"Price="<<price<<endl
<<"Color="<<color;
}
}A;
int main()

25
{
A.Apple();
A.output();
getch();
return 0;
}
Output

26
19. Multilevel Inheritance.
Source code
#include<iostream.h>
#include<conio.h>
class animal{
protected:
char name[20];
int legs;
};
class mammels:public animal
{
protected:
char color[10];
};
class cow : public mammels
{
public:
Cow()
{
cout<<"Enter name, number of legs and color of
animal.\n";
cin>>name>>legs>>color;
}
void output()
{
cout<<"Name="<<name<<endl
<<"No. of legs="<<legs<<endl
<<"Color="<<color;
}}A;
int main()
{
A.Cow();
A.output();

27
getch();
return 0;
}
Output

28
20. Multiple Inheritance.
Source code
#include <iostream.h>
#include <conio.h>
class door{
protected:
int door;
};
class engine{
protected:
char type [20];
};
class car: public door, public engine
{
public:
void input()
{
cout<<"Enter no. of door and type of engine of car.\n";
cin>>door>>type;
}

void output()
{
cout<<"The entered data are::\n";
cout<<"No. of door="<<door<<endl;
cout<<"Engine="<<type<<endl;
}
};
int main()
{
car c;
c.input();

29
c.output();
getch();
return 0;
}
Output

30
21. Hybrid inheritance.
Source code
#include <iostream.h>
#include <conio.h>
class vehicle
{
protected:
int price;
int model;
};
class light_weight : public vehicle
{
protected:
int no_of_wheels;
};
class Car : public light_weight
{
int milage;
public:
Car()
{
cout<<"Enter price, model, no. of wheels and milage"
<<"of car\n";
cin>>price>>model>>no_of_wheels>>milage;
}
void output()
{
cout<<"Car\n\n"
<<"Price="<<price<<endl
<<"Model="<<model<<endl
<<"No. of wheels"<<no_of_wheels<<endl
<<"Milage="<<milage<<endl<<endl;

31
}
};
class Motorbike : public light_weight
{
int no_of_seats;
public:
Motorbike()
{
cout<<"Enter price, model, no. of wheels and no. of"
<<"seats of bike.\n";
cin>>price>>model>>no_of_wheels>>no_of_seats;
}
void output()
{
cout<<"Bike\n\n"
<<"Price="<<price<<endl
<<"Model="<<model<<endl
<<"No. of wheels"<<no_of_wheels<<endl
<<"No. of seats="<<no_of_seats<<endl;
}
};
int main()
{
Car C;
Motorbike M;
C.output();
M.output();
getch();
return 0;
}

32
Output

33
22. Containership. (Class inside class)
Source code
#include <iostream.h>
#include <conio.h>
class B
{
public:
int a, b;
};
class A
{
B obj;
public:
void input()
{
cout<<"Enter value of a and b."<<endl;
cin>>obj.a>>obj.b;
}
void output()
{
cout<<"Value entered are::\nA="<<obj.a<<endl
<<"B="<<obj.b;
}
};
int main()
{
A object;
object.input();
object.output();
getch();
return 0;
}

34
Output

35
23. Create class Employee (eid, salary, department) with member functions to do
I/O. Now create class named person with contains object of Employee class and data
members name and age. Include member function to do I/O. Write main part to
create object of person class and call member functions to execute the program.

Source code
#include <iostream.h>

#include <conio.h>

class Employee

protected:

int eid, salary;

char department[20];

public:

void input()

cout<<"Enter eid, salary and department.\n";

cin>>eid>>salary>>department;

void output()

cout<<"\nData enterd are::\n\nEid="<<eid<<endl

<<"Salary="<<salary<<endl

<<"Department="<<department<<endl;

};

class Person { Employee E;

public:

char name[20];

int age;

36
public:

void input()

E.input();

cout<<"Enter name and age.\n";

cin>>name>>age;

void output()

E.output();

cout<<"Name="<<name<<endl

<<"Age="<<age<<endl;

}};

int main()

Person P;

P.input();

P.output();

getch();

return 0;

37
Output

38
24. Solution of ambiguity problem of multiple inheritance.
Source code
#include <iostream.h>
#include <conio.h>
class transport
{
public:
int price;
};
class bike : public transport
{};
class car : public transport
{};
class nano : public bike, public car
{};
int main ()
{
nano N;
N.bike :: price=1000000;
cout<<"Price of nano=";
cout<<N.bike :: price;
getch ();
return 0;
}
Output

39
25. Solution of ambiguity problem of multiple inheritance. (Virtual Base Class)
Source code
#include <iostream.h>

#include <conio.h>

class A

public:

int a;

};

class B: virtual public A

{};

class C: virtual public A

{};

class D:public B, public C

{};

int main ()

D obj;

obj.a=45;

cout<<"Value of a=" ;

cout<<obj.a;

getch();

return 0;

Output

40

Potrebbero piacerti anche