SlideShare a Scribd company logo
P a g e 1
S.C.D. Govt. College
Practical File
Object Oriented
Programming Using C++
Submitted to:
Er. Navneet Kaur Sandhu
Signature
Submitted by:
Jasbir Singh
BCA-2nd
sem
7009
Subject Code: BCA-16-206
P a g e 2
I have taken efforts in this practical file. I am highly indebted
to the C++ programming teacher Er. Navneet Kaur Sandhu for
her guidance and constant supervision as well as for providing
necessary information regarding the programs and also for her
support in completing the practical file.
I would like to express my gratitude towards my parents for
their kind co-operation and encouragement which helped me in
the completion of this practical file.
My thanks and appreciations also go to my classmates in
developing the practical file and to the people who have
willingly helped me out with their abilities.
Name: Jasbir SinghPlace: Ludhiana
Date: 01/04/2018
Signature
Acknowledgement
P a g e 3
S.No. Title Page No. Remarks
1. Program to add two numbers.
2. Program to print first 10 natural
numbers.
3. Program to print sum of first 10 natural
numbers.
4. Program to check whether the entered
number is prime or not.
5. Program to print factorial of a number.
6. Program to print Fibonacci series.
7. Program to check whether the number is
odd or even.
8. Program to print day of the week based
on the day number entered.
9. Program to print first 10 odd natural
numbers using while & do while loop.
10. Program to move the cursor to the next
line using endl manipulator.
11. Program to input a variable in
hexadecimal form and display the
number in different notations like
hexadecimal, octal & decimal.
12. Program to input a variable in octal form
and display the variable in different
number system using setbase(b)
manipulator.
13. Program to display a cout statement
within the width specified.
14. Program to fill empty spaces with the
specified character c using setfill()
manipulator.
15. Program to round off a number to 2 and
3 decimal places.
16. Program to calculate the area of a
rectangle using objects & classes.
Table of Contents
P a g e 4
17. Program to calculate the area of a
rectangle by defining member function
outside the class.
18. Program to calculate gross salary of an
employee by demonstrating how the
private member functions can be
accessed from within the class.
19. Program to demonstrate an array of bank
account objects.
20. Program to demonstrate passing an
object(s) by value.
21. Program to demonstrate passing an
object(s) by reference.
22. Program to add two time objects by
returning an object from a function.
23. Program to understand the concept of
static data members.
24. Program to understand the concept of
static member functions.
25. Program to show the concept of function
overloading to calculate area where
same name functions differ in number of
parameters.
26. Program to illustrate the use of
constructor member function to initialize
an object during its creation.
27. Program to initialize an object with
different set of values using
parameterized constructor.
28. Program to understand the use of copy
constructor.
29. Program to demonstrate the use of
constructor overloading and destructor.
30. Program to demonstrate how non-
member function can access private data
members of a class.
31. Program to demonstrate how a friend
function acts as a bridge between two
classes.
32. Program to illustrate that a member
function of one class can act as a friend
of another class.
P a g e 5
33. Program to illustrate how to declare the
entire class as a friend of another class.
34. Program to increment a number by 1
without overloading the operator.
35. Program to overload unary increment
operator (++).
36. Program showing overloading of prefix
and postfix increment operators.
37. Program to add two complex numbers in
which addition is performed by
overloading the binary operator +.
38. Program to overload a unary operator
using friend function.
39. Program to compare the date of birth of
two persons by overloading the equality
operator (==).
40. Program to add two time objects by
overloading += operator.
41. Program to understand how to perform
conversion of data from basic to user-
defined type.
42. Program to understand how to perform
conversion of data from user-defined
type (class) to basic type.
43. Program to convert given amount in
dollars to rupees. (Assume 1$ = Rs.
50.00)
44. Program to convert given amount in
dollars to rupees by defining conversion
function in the destination class.
(Assume 1$ = Rs. 50.00)
45. Program to convert an object of one
class to object of another class & vice-
versa.
46. Program to create a class vector and
overload >>, <<, *.
47. Program to illustrate how to derive a
class from a base class.
48. Program to implement multilevel
inheritance.
49. Program to implement hierarchical
inheritance.
P a g e 6
50. Program to demonstrate multiple
inheritance in which a class is derived
publicly from both the base classes.
51. Program to implement multiple
inheritance to calculate total revenue
generated from the sale of books.
52. Program to understand hybrid
inheritance, consider an example of
calculating the result of a student on the
basis of marks obtained in internal and
external examination.
53. Program to calculate the result of the
student using private inheritance.
54. Program to demonstrate the use of
protected members.
55. Program to illustrate the concept of
overriding member functions.
56. Program to maintain student information
by creating a composite class having one
of its data member as an object of
another predefined class.
57. Program to illustrate how base class
pointer is used in conjunction with
derived class objects.
58. Program to implement run time
polymorphism using virtual function.
P a g e 7
P a g e 8
// Program to add two numbers.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int a, b;
clrscr();
cout << "Enter value of a: ";
cin >> a;
cout << "Enter value of b: ";
cin >> b;
cout << "Sum = " << (a + b);
getch();
}
P a g e 9
P a g e 10
// Program to print first 10 natural numbers.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int i;
clrscr();
cout << "First 10 natural numbers: ";
for(i = 1; i <= 10; i++)
cout << i << " ";
getch();
}
P a g e 11
P a g e 12
// Program to print sum of first 10 natural numbers.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int i, sum = 0;
clrscr();
cout << "First 10 natural numbers: ";
for(i = 1; i <= 10; i++){
cout << i << " ";
sum += i;
}
cout << "nSum = " << sum;
getch();
}
P a g e 13
P a g e 14
// Program to check whether the entered number is prime or not.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int i, num, count = 0;
clrscr();
cout << "Enter any number: ";
cin >> num;
for(i = 1; i <= num; i++){
if((num % i) == 0)
count++;
}
if(count == 2)
cout << num << " is a prime number.";
else
cout << num << " is not a prime number.";
getch();
}
P a g e 15
P a g e 16
// Program to print factorial of a number.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int i, num, fact = 1;
cout << "nEnter any number: ";
cin >> num;
for(i = 1; i <= num; i++)
fact *= i;
cout << "Factorial of " << num << " = " << fact;
getch();
}
P a g e 17
P a g e 18
// Program to print Fibonacci series.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int i, n, a = 0, b = 1, c;
clrscr();
cout << "Enter no. of terms: ";
cin >> n;
if(n == 1)
cout << "Fibonacci series: " << a << " ";
else
cout << "Fibonacci series: " << a << " " << b << " ";
for(i = 3; i <= n; i++){
c = a + b;
cout << c << " ";
a = b;
b = c;
}
getch();
}
P a g e 19
P a g e 20
// Program to check whether the no. is odd or even.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int num;
clrscr();
cout << "Enter a number: ";
cin >> num;
if((num % 2) == 0)
cout << num << " is an even number.";
else
cout << num << " is an odd number.";
getch();
}
P a g e 21
P a g e 22
// Program to print day of the week based on the day number entered.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
int dnum;
clrscr();
cout << "Enter any day number (1-7): ";
cin >> dnum;
switch(dnum){
case 1: cout << "Sunday";
break;
case 2: cout << "Monday";
break;
case 3: cout << "Tuesday";
break;
case 4: cout << "Wednesday";
break;
case 5: cout << "Thursday";
break;
case 6: cout << "Friday";
break;
case 7: cout << "Saturday";
break;
default: cout << "Invalid day no.";
}
getch();
}
P a g e 23
P a g e 24
// Program to print first 10 odd natural numbers using while & do while
loop.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int i = 1, x = 1;
cout << "While loop: " << endl;
cout << "First 10 odd natural numbers: ";
while(i <= 10){
cout << x << " ";
x += 2;
i++;
}
i = 1; x = 1; cout << "nnDo while loop: " << endl;
cout << "First 10 odd natural numbers: ";
do{
cout << x << " ";
x += 2;
i++;
}while(i <= 10);
getch();
}
P a g e 25
P a g e 26
// Program to move the cursor to the next line using endl manipulator.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
cout << "Enter name: " << endl;
cout << "My name is Jasbir Singh.";
getch();
}
P a g e 27
P a g e 28
// Program to enter a variable in hexadecimal form and display the number
in different notations like hexadecimal, octal & decimal.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
int i;
cout << "nEnter a hexadecimal no.: ";
cin >> hex >> i;
cout << "nValue in hex format = " << hex << i;
cout << "nValue in octal format = " << oct << i;
cout << "nValue in decimal format = " << dec << i;
getch();
}
P a g e 29
P a g e 30
// Program to input a variable in octal form and display the variable in
different number system using setbase(b).
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
int num;
cout << "nEnter value of num in octal form: ";
cin >> setbase(8) >> num;
cout << "nValue in hex form = " << setbase(16) << num;
cout << "nValue in oct form = " << setbase(8) << num;
cout << "nValue in dec form = " << setbase(10) << num;
getch();
}
P a g e 31
P a g e 32
// Program to display a cout statement within the width specified.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
int age = 19, rollno = 7009;
cout << setw(12) << "My age is" << setw(8) << age << endl;
cout << setw(12) << "My rollno is" << setw(8) << rollno;
getch();
}
P a g e 33
P a g e 34
// Program to fill empty spaces with the specified character c using setfill()
manipulator.
// Written by Jasbir Singh.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
int age = 19, rollno = 7009;
cout << setw(4) << setfill('#') << age << setw(6) << rollno << endl;
cout << setw(6) << age << setw(8) << rollno;
getch();
}
P a g e 35
P a g e 36
// Program to round off a number to 2 and 3 decimal places.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main(){
clrscr();
float a = 43.455368;
cout << setprecision(2) << a << endl;
cout << setprecision(3) << a;
getch();
}
P a g e 37
P a g e 38
// Program to calculate the area of a rectangle using objects & classes.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class rectangle{
int l, b;
public:
void setdata(int x, int y){
l = x;
b = y;
}
void area(){
cout << "Area = " << (l * b) << endl;
}
};
void main(){
clrscr();
rectangle r1, r2;
cout << "nFirst rectangle " << endl;
r1.setdata(8, 3);
r1.area();
cout << "nSecond rectangle ";
cout << "nEnter length & breadth: ";
int x, y;
cin >> x >> y;
r2.setdata(x, y);
r2.area();
getch();
}
P a g e 39
P a g e 40
// Program to calculate the area of a rectangle by defining member function
outside the class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class rectangle{
int l, b;
public:
void setdata(int, int);
void area();
};
void rectangle::setdata(int x, int y){
l = x;
b = y;
}
void rectangle::area(){
cout << "Area = " << (l * b) << endl;
}
void main(){
clrscr();
rectangle r1, r2;
cout << "nFirst rectangle " << endl;
r1.setdata(8, 3);
r1.area();
cout << "nSecond rectangle ";
cout << "nEnter length & breadth: ";
int x, y;
cin >> x >> y;
r2.setdata(x, y);
r2.area();
getch();
}
P a g e 41
P a g e 42
// Program to calculate gross salary of an employee by demonstrating how
the private member functions can be accessed from within the class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class employee{
char ename[30];
int bs;
double cal_da(){
return (0.05 * bs);
}
double cal_hra(){
return (0.15 * bs);
}
public:
void setdata(void);
void total_sal(void);
};
void employee::setdata(){
cout << "Enter employee name: ";
cin >> ename;
cout << "Enter basic salary: ";
cin >> bs;
}
void employee::total_sal(){
double total;
double da = cal_da();
double hra = cal_hra();
total = bs + da + hra;
cout << "nTotal salary = " << total;
}
void main(){
clrscr();
employee e;
e.setdata();
e.total_sal();
getch();
}
P a g e 43
P a g e 44
// Program to demonstrate an array of bank account objects.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class account{
int accno;
char name[20];
double bal;
public:
void input_data();
void display_data();
};
void account::input_data(){
cout << "n-------------------------n";
cout << "Enter account no.: ";
cin >> accno;
cout << "Enter name: ";
cin >> name;
cout << "Enter balance: ";
cin >> bal;
}
void account::display_data(){
cout << "n-------------------------";
cout << "nAccount no.= " << accno;
cout << "nName = " << name;
cout << "nBalance = " << bal;
}
void main(){
clrscr();
account a[30];
int n;
cout << "nEnter the no. of persons whose info you want to add: ";
cin >> n;
for(int i = 0; i < n; i++){
cout << "nEnter info of " << (i + 1) << " person: ";
a[i].input_data();
}
for(i = 0; i < n; i++){
cout << "nPerson " << (i + 1) << " details: ";
a[i].display_data();
}
getch();
}
P a g e 45
P a g e 46
// Program to demonstrate passing an object(s) by value.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class time{
int hrs, mins;
public:
void read_time();
void show_time();
void add_time(time, time);
};
void time::read_time(){
cout << "nEnter hours & minutes: ";
cin >> hrs >> mins;
}
void time::show_time(){
cout << "nTime is " << hrs << ":" << mins;
}
void time::add_time(time t1, time t2){
int m = t1.mins + t2. mins;
int h = (m/60);
mins = (m%60);
hrs = t1.hrs + t2.hrs + h;
}
void main(){
clrscr();
time t1, t2, t3;
cout << "nEnter 1st time: ";
t1.read_time();
cout << "nEnter 2nd time: ";
t2.read_time();
t3.add_time(t1, t2);
cout << "nFinal time: ";
t3.show_time();
getch();
}
P a g e 47
P a g e 48
// Program to demonstrate passing an object(s) by reference.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class account{
int accno;
float balance;
public:
void read();
void show();
void transfer(account &, double);
};
void account::read(){
cout << "nEnter account no. = ";
cin >> accno;
cout << "Enter balance = ";
cin >> balance;
}
void account::show(){
cout << "nAccount no. = " << accno;
cout << "nBalance = " << balance;
}
void account::transfer(account &act, double amt){
balance -= amt;
act.balance += amt;
}
void main(){
clrscr();
account a1, a2;
cout << "Enter 1st account info: ";
a1.read();
cout << "Enter 2nd account info: ";
a2.read();
cout << "Enter amount to be transferred from account 1 to account 2: ";
double money;
cin >> money;
a1.transfer(a2, money);
cout << "nDisplaying 1st account info: ";
a1.show();
cout << "nDisplaying 2nd account info: ";
a2.show();
getch();
}
P a g e 49
P a g e 50
// Program to add two time objects by returning an object from a function.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class time{
int hrs, mins;
public:
void read_time();
void show_time();
time add_time(time);
};
void time::read_time(){
cout << "nEnter hours & minutes: ";
cin >> hrs >> mins;
}
void time::show_time(){
cout << "nTime is " << hrs << ":" << mins;
}
time time::add_time(time t){
time temp;
int m = mins + t.mins;
int h = (m/60);
temp.mins = (m%60);
temp.hrs = hrs + t.hrs + h;
return temp;
}
void main(){
clrscr();
time t1, t2, t3;
cout << "nEnter 1st time: ";
t1.read_time();
cout << "nEnter 2nd time: ";
t2.read_time();
t3 = t1.add_time(t2);
cout << "nTime after adding: ";
t3.show_time();
getch();
}
P a g e 51
P a g e 52
// Program to understand the concept of static data members.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class account{
int accno;
double bal;
static double rate;
public:
void read();
void show();
void qtr_rate_call();
};
void account::read(){
cout << "nEnter account no.: ";
cin >> accno;
cout << "Enter balance: ";
cin >> bal;
}
void account::show(){
cout << "nAccount No. = " << accno;
cout << "nBalance = " << bal;
}
double account::rate = 0.05;
void account::qtr_rate_call(){
double interest = (bal * rate * 0.25);
bal += interest;
}
void main(){
clrscr();
account a1, a2;
cout << "nEnter 1st account info: ";
a1.read();
cout << "nEnter 2nd account info: ";
a2.read();
a1.qtr_rate_call();
a2.qtr_rate_call();
cout << "nAfter giving quarter interest: ";
a1.show();
a2.show();
getch();
}
P a g e 53
P a g e 54
// Program to understand the concept of static data members.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class account{
int accno;
double bal;
static double rate;
public:
void read();
void show();
void qtr_rate_call();
};
void account::read(){
cout << "nEnter account no.: ";
cin >> accno;
cout << "Enter balance: ";
cin >> bal;
}
void account::show(){
cout << "nAccount No. = " << accno;
cout << "nBalance = " << bal;
}
double account::rate = 0.05;
void account::qtr_rate_call(){
double interest = (bal * rate * 0.25);
bal += interest;
}
void main(){
clrscr();
account a1, a2;
cout << "nEnter 1st account info: ";
a1.read();
cout << "nEnter 2nd account info: ";
a2.read();
a1.qtr_rate_call();
a2.qtr_rate_call();
cout << "nAfter giving quarter interest: ";
a1.show();
a2.show();
getch();
}
P a g e 55
P a g e 56
// Program to show the concept of function overloading to calculate area
where same name functions differ in number of parameters.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void area(int);
void area(int, int);
void area(int, int, int);
void main(){
clrscr();
int s, l, b, s1, s2, s3;
cout << "nEnter side of square: ";
cin >> s;
area(s);
cout << "nEnter length & breadth of a rectangle: ";
cin >> l >> b;
area(l, b);
cout << "nEnter sides of triangle: ";
cin >> s1 >> s2 >> s3;
area(s1, s2, s3);
getch();
}
void area(int x){
cout << "Area = " << (x * x) << endl;
}
void area(int x, int y){
cout << "Area = " << (x * y) << endl;
}
void area(int x, int y, int z){
float sp = (x + y + z)/2;
float ar = sqrt(sp * (sp - x) * (sp - y) * (sp - z));
cout << "Area = " << ar << endl;
}
P a g e 57
P a g e 58
// Program to illustrate the use of constructor member function to initialize
an object during its creation.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class rectangle{
int l, b;
public:
rectangle(){
l = 5;
b = 3;
}
int area(){
return (l * b);
}
};
void main(){
clrscr();
rectangle r;
cout << "nArea of rectangle = " << r.area();
getch();
}
P a g e 59
P a g e 60
// Program to initialize an object with different set of values using
parameterized constructor.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class rectangle{
int l, b;
public:
rectangle(){
l = b = 0;
}
rectangle(int x, int y){
l = x;
b = y;
}
int area(){
return (l * b);
}
};
void main(){
clrscr();
rectangle r1(6, 4), r2(9, 5);
cout << "nArea of 1st rectangle = " << r1.area();
cout << "nArea of 2nd rectangle = " << r2.area();
getch();
}
P a g e 61
P a g e 62
// Program to understand the concept of copy constructor.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class count{
int c;
public:
count(){}
count(int x){
c = x;
}
count(count &obj){
c = obj.c;
cout << "nCopy constructor invoked.";
}
void show(){
cout << "nCount = " << c;
}
};
void main(){
clrscr();
count c1, c2(5), c3(c2);
c1.show();
c2.show();
c3.show();
getch();
}
P a g e 63
P a g e 64
// Program to demonstrate the use of constructor overloading and
destructor.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class rectangle{
int l, b;
public:
rectangle(){
l = b = 0;
cout << "nDefault constuctor called.";
}
rectangle(int x){
l = b = x;
cout << "nConstructor with one parameter called.";
}
rectangle(int x, int y){
l = x;
b = y;
cout << "nConstructor with two parameters called.";
}
rectangle(rectangle &ob){
l = ob.l;
b = ob.b;
cout << "nCopy constructor called.";
}
int area(){
return (l * b);
}
~rectangle(){
cout << "nDestructor called.";
cout << "nreleasing memory......" ;
}
};
void main(){
clrscr();
rectangle r1, r2(4), r3(6, 3), r4(r2);
cout << "nArea of 1st rectangle = " << r1.area();
cout << "nArea of 2nd rectangle = " << r2.area();
cout << "nArea of 3rd rectangle = " << r3.area();
cout << "nArea of 4th rectangle = " << r4.area();
getch();
}
P a g e 65
P a g e 66
// Program to demonstrate how non-member function can access private
data members of a class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class A{
int a;
public:
A(int x){
a = x;
}
friend void display(A);
};
void display(A obj){
cout << "nA = " << obj.a;
}
void main(){
clrscr();
A ob(5);
display(ob);
getch();
}
P a g e 67
P a g e 68
// Program to demonstrate how a friend function acts as a bridge between
two classes.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class B;
class A{
int a;
public:
A(int x){
a = x;
}
friend void diff(A, B);
};
class B{
int b;
public:
B(int y){
b = y;
}
friend void diff(A, B);
};
void diff(A obj1, B obj2){
cout << "nDifference = " << (obj1.a - obj2.b);
}
void main(){
clrscr();
A obj1(8);
B obj2(5);
diff(obj1, obj2);
getch();
}
P a g e 69
P a g e 70
// Program to illustrate that a member function of one class can act as a
friend of another class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class B;
class A{
int a;
public:
A(int x){
a = x;
}
void diff(B);
};
class B{
int b;
public:
B(int y){
b = y;
}
friend void A::diff(B);
};
void A::diff(B obj){
cout << "nDifference = " << (a - obj.b);
}
void main(){
clrscr();
A obj1(8);
B obj2(5);
obj1.diff(obj2);
getch();
}
P a g e 71
P a g e 72
// Program to illustrate how to declare the entire class as a friend of
another class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class A{
int a, b;
public:
A(int x, int y){
a = x;
b = y;
}
friend class B;
};
class B{
public:
void display_a(A obj){
cout << "na = " << obj.a;
}
void display_b(A obj){
cout << "nb = " << obj.b;
}
};
void main(){
clrscr();
A obj1(10, 15);
B obj2();
obj2.display_a(obj1);
obj2.display_b(obj1);
getch();
}
P a g e 73
P a g e 74
// Program to increment a number by 1 without overloading the operator.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class score{
int val;
public:
score(){
val = 0;
}
void increment(){
val++;
}
int show(){
return val;
}
};
void main(){
clrscr();
score s1, s2;
cout << "nInitial value of s1 = " << s1.show();
cout << "nInitial value of s2 = " << s2.show();
s1.increment();
s1.increment();
s2.increment();
cout << "nFinal value of s1 = " << s1.show();
cout << "nFinal value of s2 = " << s2.show();
getch();
}
P a g e 75
P a g e 76
// Program to overload unary increment operator (++).
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class score{
int val;
public:
score(){
val = 0;
}
void operator++(){
val++;
}
int show(){
return val;
}
};
void main(){
clrscr();
score s1, s2;
cout << "nInitial value of s1 = " << s1.show();
cout << "nInitial value of s2 = " << s2.show();
s1++;
++s1;
s2++;
cout << "nFinal value of s1 = " << s1.show();
cout << "nFinal value of s2 = " << s2.show();
getch();
}
P a g e 77
P a g e 78
// Program showing overloading of prefix and postfix increment operators.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class score{
int val;
public:
score(){
val = 0;
}
score operator++(){
score temp;
val = val + 1;
temp.val = val;
return temp;
}
score operator++(int){
score temp;
temp.val = val;
val = val + 1;
return temp;
}
int show(){
return val;
}
};
void main(){
clrscr();
score s1, s2;
cout << "nInitial value of s1 = " << s1.show();
cout << "nInitial value of s2 = " << s2.show();
s2 = s1++;
s1 = ++s2;
s1 = s1++;
cout << "nFinal value of s1 = " << s1.show();
cout << "nFinal value of s2 = " << s2.show();
getch();
}
P a g e 79
P a g e 80
// Program to add two complex numbers in which addition is performed by
// overloading the binary operator +.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class complex{
double real, imag;
public:
complex(){
real = imag = 0.0;
}
void read(){
cout << "nEnter real & imaginary part: ";
cin >> real >> imag;
}
complex operator+(complex obj){
complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void show();
};
void complex::show(){
if(imag > 0)
cout << real << "+" << imag << "i";
else if(imag == 0)
cout << real;
else
cout << real << imag << "i";
}
void main(){
clrscr();
complex c1, c2, c3;
cout << "nEnter 1st complex no.: ";
c1.read();
cout << "nEnter 2nd complex no.: ";
c2.read();
c3 = c1 + c2;
cout << "nSum of two complex numbers = ";
c3.show();
getch();
}
P a g e 81
P a g e 82
// Program to overload a unary operator using friend function.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class score{
int val;
public:
score(){
val = 0;
}
int show(){
return val;
}
friend score operator++(score &);
friend score operator++(score &, int);
};
void main(){
clrscr();
score s1, s2;
cout << "nInitial value of s1 = " << s1.show();
cout << "nInitial value of s2 = " << s2.show();
s2 = s1++;
s1 = ++s2;
s1 = s1++;
cout << "nFinal value of s1 = " << s1.show();
cout << "nFinal value of s2 = " << s2.show();
getch();
}
score operator++(score &obj){
score temp;
obj.val = obj.val + 1;
temp.val = obj.val;
return temp;
}
score operator++(score &obj, int){
score temp;
temp.val = obj.val;
obj.val = obj.val + 1;
return temp;
}
P a g e 83
P a g e 84
// Program to compare the date of birth of two persons by overloading the
equality operator (==).
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class dob{
int dd, mm, yy;
public:
dob(int d, int m, int y){
dd = d;
mm = m;
yy = y;
}
void show(){
cout << dd << "-" << mm << "-" << yy;
}
int operator==(dob);
};
int dob::operator==(dob obj){
if((dd == obj.dd) && (mm == obj.mm) && (yy == obj.yy))
return 1;
else
return 0;
}
void main(){
clrscr();
dob d1(14, 5, 2018), d2(1, 1, 1980), d3(14, 5, 2018);
if(d1 == d2)
cout << "nTwo dates are equal - d1 & d2.";
else
cout << "nTwo dates are not equal.";
if(d1 == d3)
cout << "nTwo dates are equal - d1 & d3.";
else
cout << "nTwo dates are not equal.";
getch();
}
P a g e 85
P a g e 86
// Program to add two time objects by overloading += operator.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class time{
int hrs, mins, secs;
public:
time(int h, int m, int s){
hrs = h;
mins = m;
secs = s;
}
void show(){
cout << "nTime is -> " << hrs << ":" << mins << ":" << secs;
}
void operator+=(time);
};
void time::operator+=(time t){
secs += t.secs;
mins += t.mins;
hrs += t.hrs;
if(secs >= 60){
secs -= 60;
mins++;
}
if(mins >= 60){
mins -= 60;
hrs++;
}
}
void main(){
clrscr();
time t1(4, 50, 32), t2(6, 30, 20);
t1.show();
t1 += t2;
cout << "nTime after overloading += ";
t1.show();
getch();
}
P a g e 87
P a g e 88
// Program to understand how to perform conversion of data from basic to
user-defined type.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class time{
int hrs, mins;
public:
time(int t){
hrs = t/60;
mins = t%60;
}
void show(){
cout << "nTime -> " << hrs << ":" << mins;
}
};
void main(){
clrscr();
int m = 96;
time t1 = m;
t1.show();
getch();
}
P a g e 89
P a g e 90
// Program to understand how to perform conversion of data from user-
defined (class) to basic type.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class time{
int hrs, mins;
public:
time(int h, int m){
hrs = h;
mins = m;
}
void show(){
cout << "nTime -> " << hrs << ":" << mins;
}
operator int(){
return (hrs * 60) + mins;
}
};
void main(){
clrscr();
time t(1, 36);
int minutes = t;
cout << "nTime in hrs & mins form: ";
t.show();
cout << "nTime in minutes only: " << minutes;
getch();
}
P a g e 91
P a g e 92
// Program to convert given amount in dollars to rupees. (Assume 1$ = Rs.
50.00)
// Written by Jasbir Singh.
#define dol_val 50.00
#include<iostream.h>
#include<conio.h>
class rupee{
double rs;
public:
rupee(){
rs = 0;
}
rupee(double r){
rs = r;
}
void show(){
cout << "nMoney in Rs. = " << rs;
}
};
class dollar{
double dol;
public:
dollar(double doll){
dol = doll;
}
void show(){
cout << "nMoney in $ = " << dol;
}
operator rupee(){
return (rupee (dol * dol_val));
}
};
void main(){
clrscr();
system("color f8");
dollar d(3);
d.show();
rupee r;
r = d;
r.show();
getch();
}
P a g e 93
P a g e 94
// Program to convert given amount in dollars to rupees by defining
conversion function in the destination class. (Assume 1$ = Rs. 50.00)
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
#define dol_val 50.00
class dollar{
double dol;
public:
dollar(double doll){
dol = doll;
}
void show(){
cout << "nMoney in dollars = " << dol;
}
double ret_dol(){
return dol;
}
};
class rupee{
double rs;
public:
rupee(){}
rupee(dollar d){
rs = d.ret_dol() * dol_val;
}
void show(){
cout << "nMoney in rupees = " << rs;
}
};
void main(){
clrscr();
dollar d(3);
rupee r;
d.show();
r = d;
r.show();
getch();
}
P a g e 95
P a g e 96
// Program to convert an object of one class to object of another class &
vice-versa.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
#define dol_val 50.00
class rupee{
double rs;
public:
rupee(){}
rupee(double r){
rs = r;
}
void show(){
cout << "nMoney in rupees = " << rs;
}
double ret_rs(){
return rs;
}
};
class dollar{
double dol;
public:
dollar(){}
dollar(double doll){
dol = doll;
}
dollar(rupee r){
dol = r.ret_rs() / dol_val;
}
operator rupee(){
return(rupee (dol * dol_val));
}
void show(){
cout << "nMoney in dollars = " << dol;
}
};
void main(){
clrscr();
dollar d1(3), d2;
rupee r1(150), r2;
d1.show();
r2 = d1;
P a g e 97
r2.show();
r1.show();
d2 = r1;
d2.show();
getch();
}
P a g e 98
P a g e 99
// Program to create a class vector and overload >>, <<, *.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
const int size = 5;
class vector{
int v[size];
public:
vector();
vector(int *);
vector operator*(int);
friend vector operator*(int, vector);
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 vector::operator*(int j){
vector temp;
for(int i = 0; i < size; i++)
temp.v[i] = v[i] * j;
return temp;
}
vector operator*(int j, vector a){
vector temp;
for(int i = 0; i < size; i++)
temp.v[i] = j * a.v[i];
return temp;
}
istream & operator>>(istream &din, vector &a){
for(int i = 0; i < size; i++)
din >> a.v[i];
return din;
}
ostream & operator<<(ostream &dout, vector &a){
dout << "(" << a.v[0];
for(int i = 1; i < size; i++)
P a g e 100
dout << ", " << a.v[i];
dout << ")";
}
void main(){
clrscr();
vector v1;
int x[] = {1, 2, 3, 4, 5};
vector v2(x);
v1 = 2 * v2;
vector v3;
cout << "nEnter vector: ";
cin >> v3;
cout << "nVector entered";
cout << "nVector v1 after multiplication: " << v1;
cout << "nVector v2: " << v2;
cout << "nVector v3: " << v3;
getch();
}
P a g e 101
P a g e 102
// Program to illustrate how to derive a class from a base class.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
class base_class{
int num1;
public:
void base_read(){
cout << "nEnter the value for num1: ";
cin >> num1;
}
void base_show(){
cout << "nNum1 = " << num1;
}
};
class der_class: public base_class{
int num2;
public:
void der_read(){
cout << "nEnter the value for num2: ";
cin >> num2;
}
void der_show(){
cout << "nNum2 = " << num2;
}
};
void main(){
clrscr();
der_class d;
d.base_read();
d.der_read();
d.base_show();
d.der_show();
getch();
}
P a g e 103
P a g e 104
// Program to implement multilevel inheritance.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class person{
char name[20];
long int phno;
public:
void read(){
cout << "nEnter name & phone no.: ";
cin >> name >> phno;
}
void show(){
cout << "nName = " << name;
cout << "nPhone No. = " << phno;
}
};
class student: public person{
int rollno;
char course[20];
public:
void read(){
person::read();
cout << "nEnter roll no. & course: ";
cin >> rollno >> course;
}
void show(){
person::show();
cout << "nRoll No. = " << rollno;
cout << "nCourse = " << course;
}
};
class exam: public student{
int m[4];
double per;
public:
void read();
void show();
void cal();
};
void exam::read(){
student::read();
cout << "nEnter marks: ";
P a g e 105
for(int i = 0; i < 4; i++)
cin >> m[i];
}
void exam::show(){
student::show();
cout << "nMarks are: ";
for(int i = 0; i < 4; i++)
cout << m[i] << "t";
cout << "nPercantage = " << per;
}
void exam::cal(){
int tm = 0;
for(int i = 0; i < 4; i++)
tm += m[i];
per = double (tm) / 4;
}
void main(){
clrscr();
exam e;
e.read();
e.cal();
e.show();
getch();
}
P a g e 106
P a g e 107
// Program to implement hierarchical inheritance.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class person{
char name[20];
long int phno;
public:
void read(){
cout << "nEnter name & phone no.: ";
cin >> name >> phno;
}
void show(){
cout << "nName = " << name;
cout << "nPhone No. = " << phno;
}
};
class student: public person{
int rollno;
char course[20];
public:
void read(){
person::read();
cout << "nEnter roll no. & course: ";
cin >> rollno >> course;
}
void show(){
person::show();
cout << "nRoll No. = " << rollno;
cout << "nCourse = " << course;
}
};
class teacher: public person{
char dept[20];
char qual[10];
public:
void read(){
person::read();
cout << "nEnter department & qualification: ";
cin >> dept >> qual;
}
void show(){
person::show();
P a g e 108
cout << "nDepartment = " << dept;
cout << "nQualification = " << qual;
}
};
void main(){
clrscr();
student s;
teacher t;
cout << "nEnter student's information: ";
s.read();
cout << "nEnter teacher's information: ";
t.read();
cout << "nDisplaying student's details: ";
s.show();
cout << "nDisplaying teacher's details: ";
t.show();
getch();
}
P a g e 109
P a g e 110
// Program to demonstrate multiple inheritance in which a class is derived
publicly from both the base classes.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
class base1{
protected:
int x;
public:
void readx(){
cout << "nEnter the value of x: ";
cin >> x;
}
void showx(){
cout << "nx = " << x;
}
};
class base2{
protected:
int y;
public:
void ready(){
cout << "nEnter the value of y: ";
cin >> y;
}
void showy(){
cout << "ny = " << y;
}
};
class der: public base1, public base2{
int z;
public:
void cal(){
z = x + y;
}
void showz(){
cout << "nz = " << z;
}
};
void main(){
clrscr();
der d;
d.readx();
P a g e 111
d.ready();
d.cal();
d.showx();
d.showy();
d.showz();
getch();
}
P a g e 112
P a g e 113
// Program to implement multiple inheritance to calculate total revenue
generated from the sale of books.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
class publication{
char publisher[20];
protected:
double price;
public:
void read(){
cout << "nEnter publisher & price: ";
cin >> publisher >> price;
}
void show(){
cout << "nPublisher = " << publisher;
cout << "nPrice = " << price;
}
};
class sales{
protected:
double pub_sales;
public:
void read(){
cout << "nEnter total sales: ";
cin >> pub_sales;
}
void show(){
cout << "nTotal sales = " << pub_sales;
}
};
class book: public publication, public sales{
char author[20];
int pages;
long isbn;
public:
void read(){
cout << "nEnter author, no. of pages, isbn num: ";
cin >> author >> pages >> isbn;
}
void show(){
cout << "nAuthor's name = " << author;
cout << "nNo. of pages = " << pages;
P a g e 114
cout << "nISBN number = " << isbn;
}
void cal_salamt(){
double amt = price * pub_sales;
cout << "nSales Amount = " << amt;
}
};
void main(){
clrscr();
book b;
cout << "nEnter publication details: ";
b.publication::read();
cout << "nEnter sales details: ";
b.sales::read();
cout << "nEnter book details: ";
b.read();
b.cal_salamt();
cout << "nDetails are: ";
b.publication::show();
b.sales::show();
b.show();
getch();
}
P a g e 115
P a g e 116
// Program to understand hybrid inheritance, consider an example of
calculating the result of a student on the basis of marks obtained in internal
and external examination.
// Written by Jasbir Singh
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
public:
void read(){
cout << "nEnter rollno & name: ";
cin >> rollno >> name;
}
void show(){
cout << "nRoll no = " << rollno;
cout << "nName = " << name;
}
};
class internal: public student{
protected:
int sub1, sub2;
public:
void read_marks(){
cout << "nEnter internal marks of subject 1 (MAX 40): ";
cin >> sub1;
cout << "nEnter internal marks of subject 2 (MAX 40): ";
cin >> sub2;
}
void display_marks(){
cout << "nInternal marks of subject 1 = " << sub1;
cout << "nInternal marks of subject 2 = " << sub2;
}
};
class external{
protected:
int sub1, sub2;
public:
void read_marks(){
cout << "nEnter external marks of subject 1 (MAX 60): ";
cin >> sub1;
cout << "nEnter external marks of subject 2 (MAX 60): ";
cin >> sub2;
P a g e 117
}
void display_marks(){
cout << "nExternal marks of subject 1 = " << sub1;
cout << "nExternal marks of subject 2 = " << sub2;
}
};
class result: public internal, public external{
int total_marks;
public:
void cal_result(){
total_marks = internal::sub1 + internal::sub2 + external::sub1 +
external::sub2;
cout << "nTotal marks obtained = " << total_marks;
}
};
void main(){
clrscr();
result r1;
cout << "nEnter student information: ";
r1.read();
cout << "nEnter marks of internal examination: ";
r1.internal::read_marks();
cout << "nEnter marks of external examination: ";
r1.external::read_marks();
cout << "nDisplaying student details: ";
r1.show();
r1.internal::display_marks();
r1.external::display_marks();
cout << "nCalculating & displaying result: ";
r1.cal_result();
getch();
}
P a g e 118
P a g e 119
// Program to calculate the result of the student using private inheritance.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class stud_base{
int rollno;
char name[20];
public:
void read(){
cout << "nEnter roll no. & name: ";
cin >> rollno >> name;
}
void show(){
cout << "nRoll no. = " << rollno;
cout << "nName = " << name;
}
};
class result: private stud_base{
int m[4];
double per;
public:
void input();
void cal();
void display();
};
void result::input(){
read();
cout << "nEnter marks: n";
for(int i = 0; i < 4; i++)
cin >> m[i];
}
void result::cal(){
int total = 0;
for(int i = 0; i < 4; i++)
total += m[i];
per = double (total)/ 4;
}
void result::display(){
show();
cout << "nMarks are: n";
for(int i = 0; i < 4; i++)
cout << m[i] << endl;
cout << "nPercentage = " << per;
P a g e 120
}
void main(){
clrscr();
result r;
clrscr();
r.input();
r.cal();
r.display();
getch();
}
P a g e 121
P a g e 122
// Program to demonstrate the use of protected members.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class num{
protected:
int x, y;
public:
void read(){
cout << "nEnter two numbers: ";
cin >> x >> y;
}
void show(){
cout << "nx = " << x;
cout << "ny = " << y;
}
};
class result: public num{
int z;
public:
void add(){
z = x + y;
}
void showz(){
cout << "nz = " << z;
}
};
void main(){
clrscr();
result r;
r.read();
r.add();
r.show();
r.showz();
getch();
}
P a g e 123
P a g e 124
// Program to illustrate the concept of overriding member functions.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class person{
long int phno;
char name[20];
public:
void read(){
cout << "nEnter name & phone no.: ";
cin >> name >> phno;
}
void show(){
cout << "nName = " << name;
cout << "nPhone no. = " << phno;
}
};
class student: public person{
int rollno;
char course[20];
public:
void read(){
person::read();
cout << "nEnter rollno. & course: ";
cin >> rollno >> course;
}
void show(){
person::show();
cout << "nRoll no. = " << rollno;
cout << "nCourse = " << course;
}
};
void main(){
clrscr();
student s;
s.read();
s.show();
getch();
}
P a g e 125
P a g e 126
// Program to maintain student information by creating a composite class
having one of its data member as an object of another predefined class.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
class dob{
int dd, mm, yy;
public:
void read(){
cout << "nEnter date, month & year: ";
cin >> dd >> mm >> yy;
}
void show(){
cout << "nDate = " << dd << "/" << mm << "/" << yy;
}
}d;
public:
void read(){
cout << "nEnter roll no. & name: ";
cin >> rollno >> name;
d.read();
}
void show(){
cout << "nRoll no. = " << rollno;
cout << "nName = " << name;
d.show();
}
};
void main(){
clrscr();
student s;
s.read();
s.show();
getch();
}
P a g e 127
P a g e 128
// Program to illustrate how base class pointer is used in conjunction with
derived class objects.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class base{
public:
void display(){
cout << "nBase class display called.";
}
};
class der1: public base{
public:
void display(){
cout << "nDer1's display called.";
}
};
class der2: public base{
public:
void display(){
cout << "nDer2's display called.";
}
};
void main(){
clrscr();
base *ptr;
der1 d1;
der2 d2;
ptr->display();
ptr = &d1;
ptr->display();
ptr = &d2;
ptr->display();
getch();
}
P a g e 129
P a g e 130
// Program to implement run time polymorphism using virtual function.
// Written by Jasbir Singh.
#include<iostream.h>
#include<conio.h>
class base{
public:
virtual void display(){
cout << "nBase class display called.";
}
};
class der1: public base{
public:
void display(){
cout << "nDer1's display called.";
}
};
class der2: public base{
public:
void display(){
cout << "nDer2's display called.";
}
};
void main(){
clrscr();
base *ptr;
der1 d1;
der2 d2;
ptr = &d1;
ptr->display();
ptr = &d2;
ptr->display();
getch();
}

More Related Content

What's hot (20)

PDF
Data Structures Practical File
Harjinder Singh
 
PPTX
Call by value
Dharani G
 
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PDF
Differences between c and c++
starlit electronics
 
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
DOCX
Let us c (by yashvant kanetkar) chapter 1 solution
Hazrat Bilal
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
C tokens
Manu1325
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
PDF
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
PPTX
C language ppt
Ğäùråv Júñêjå
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PDF
Character Array and String
Tasnima Hamid
 
PPTX
Conditional Statement in C Language
Shaina Arora
 
DOCX
C++ file
Mukund Trivedi
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPT
Array in c
Ravi Gelani
 
Data Structures Practical File
Harjinder Singh
 
Call by value
Dharani G
 
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
Data types in c++
Venkata.Manish Reddy
 
Differences between c and c++
starlit electronics
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Let us c (by yashvant kanetkar) chapter 1 solution
Hazrat Bilal
 
07. Virtual Functions
Haresh Jaiswal
 
C tokens
Manu1325
 
Practical File of C Language
RAJWANT KAUR
 
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
C language ppt
Ğäùråv Júñêjå
 
Function in C program
Nurul Zakiah Zamri Tan
 
Character Array and String
Tasnima Hamid
 
Conditional Statement in C Language
Shaina Arora
 
C++ file
Mukund Trivedi
 
Functions in c
sunila tharagaturi
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Array in c
Ravi Gelani
 

Similar to Object Oriented Programming Using C++ Practical File (20)

DOCX
Practical File of c++.docx lab manual program question
vidhimangal05
 
DOCX
Oops practical file
Ankit Dixit
 
DOCX
programming
zohaib arif
 
PDF
Basic Elements of C++
Jason J Pulikkottil
 
PDF
EEE 3rd year oops cat 3 ans
Karthik Venkatachalam
 
PDF
How to program in c++ with 100 examples
Shahid Riaz
 
PPT
Apclass (2)
geishaannealagos
 
DOCX
Basic Programs of C++
Bharat Kalia
 
PPT
Apclass
geishaannealagos
 
DOC
Pads lab manual final
AhalyaR
 
PDF
c++ lab manual
Shrunkhala Wankhede
 
PDF
c++ practical Digvajiya collage Rajnandgaon
yash production
 
PDF
C++ practical
Rahul juneja
 
DOCX
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Make Mannan
 
PPT
Apclass (2)
geishaannealagos
 
DOC
project report in C++ programming and SQL
vikram mahendra
 
DOCX
Computer Practical XII
Ûťţåm Ğűpţä
 
DOCX
Opp compile
Muhammad Faiz
 
PDF
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
jovaerili
 
Practical File of c++.docx lab manual program question
vidhimangal05
 
Oops practical file
Ankit Dixit
 
programming
zohaib arif
 
Basic Elements of C++
Jason J Pulikkottil
 
EEE 3rd year oops cat 3 ans
Karthik Venkatachalam
 
How to program in c++ with 100 examples
Shahid Riaz
 
Apclass (2)
geishaannealagos
 
Basic Programs of C++
Bharat Kalia
 
Pads lab manual final
AhalyaR
 
c++ lab manual
Shrunkhala Wankhede
 
c++ practical Digvajiya collage Rajnandgaon
yash production
 
C++ practical
Rahul juneja
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Make Mannan
 
Apclass (2)
geishaannealagos
 
project report in C++ programming and SQL
vikram mahendra
 
Computer Practical XII
Ûťţåm Ğűpţä
 
Opp compile
Muhammad Faiz
 
CBSE Question Paper Computer Science with C++ 2011
Deepak Singh
 
C++ How to Program 10th Edition Deitel Solutions Manual
jovaerili
 
Ad

Recently uploaded (20)

PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
How to use grouped() method in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
DOCX
ANNOTATION on objective 10 on pmes 2022-2025
joviejanesegundo1
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
DOCX
DLL english grade five goof for one week
FlordelynGonzales1
 
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
PPTX
ENGLISH -PPT- Week1 Quarter1 -day-1.pptx
garcialhavz
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
How to Add New Item in CogMenu in Odoo 18
Celine George
 
How to use grouped() method in Odoo 18 - Odoo Slides
Celine George
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
ANNOTATION on objective 10 on pmes 2022-2025
joviejanesegundo1
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
DLL english grade five goof for one week
FlordelynGonzales1
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
VCE Literature Section A Exam Response Guide
jpinnuck
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
ENGLISH -PPT- Week1 Quarter1 -day-1.pptx
garcialhavz
 
Ad

Object Oriented Programming Using C++ Practical File

  • 1. P a g e 1 S.C.D. Govt. College Practical File Object Oriented Programming Using C++ Submitted to: Er. Navneet Kaur Sandhu Signature Submitted by: Jasbir Singh BCA-2nd sem 7009 Subject Code: BCA-16-206
  • 2. P a g e 2 I have taken efforts in this practical file. I am highly indebted to the C++ programming teacher Er. Navneet Kaur Sandhu for her guidance and constant supervision as well as for providing necessary information regarding the programs and also for her support in completing the practical file. I would like to express my gratitude towards my parents for their kind co-operation and encouragement which helped me in the completion of this practical file. My thanks and appreciations also go to my classmates in developing the practical file and to the people who have willingly helped me out with their abilities. Name: Jasbir SinghPlace: Ludhiana Date: 01/04/2018 Signature Acknowledgement
  • 3. P a g e 3 S.No. Title Page No. Remarks 1. Program to add two numbers. 2. Program to print first 10 natural numbers. 3. Program to print sum of first 10 natural numbers. 4. Program to check whether the entered number is prime or not. 5. Program to print factorial of a number. 6. Program to print Fibonacci series. 7. Program to check whether the number is odd or even. 8. Program to print day of the week based on the day number entered. 9. Program to print first 10 odd natural numbers using while & do while loop. 10. Program to move the cursor to the next line using endl manipulator. 11. Program to input a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. 12. Program to input a variable in octal form and display the variable in different number system using setbase(b) manipulator. 13. Program to display a cout statement within the width specified. 14. Program to fill empty spaces with the specified character c using setfill() manipulator. 15. Program to round off a number to 2 and 3 decimal places. 16. Program to calculate the area of a rectangle using objects & classes. Table of Contents
  • 4. P a g e 4 17. Program to calculate the area of a rectangle by defining member function outside the class. 18. Program to calculate gross salary of an employee by demonstrating how the private member functions can be accessed from within the class. 19. Program to demonstrate an array of bank account objects. 20. Program to demonstrate passing an object(s) by value. 21. Program to demonstrate passing an object(s) by reference. 22. Program to add two time objects by returning an object from a function. 23. Program to understand the concept of static data members. 24. Program to understand the concept of static member functions. 25. Program to show the concept of function overloading to calculate area where same name functions differ in number of parameters. 26. Program to illustrate the use of constructor member function to initialize an object during its creation. 27. Program to initialize an object with different set of values using parameterized constructor. 28. Program to understand the use of copy constructor. 29. Program to demonstrate the use of constructor overloading and destructor. 30. Program to demonstrate how non- member function can access private data members of a class. 31. Program to demonstrate how a friend function acts as a bridge between two classes. 32. Program to illustrate that a member function of one class can act as a friend of another class.
  • 5. P a g e 5 33. Program to illustrate how to declare the entire class as a friend of another class. 34. Program to increment a number by 1 without overloading the operator. 35. Program to overload unary increment operator (++). 36. Program showing overloading of prefix and postfix increment operators. 37. Program to add two complex numbers in which addition is performed by overloading the binary operator +. 38. Program to overload a unary operator using friend function. 39. Program to compare the date of birth of two persons by overloading the equality operator (==). 40. Program to add two time objects by overloading += operator. 41. Program to understand how to perform conversion of data from basic to user- defined type. 42. Program to understand how to perform conversion of data from user-defined type (class) to basic type. 43. Program to convert given amount in dollars to rupees. (Assume 1$ = Rs. 50.00) 44. Program to convert given amount in dollars to rupees by defining conversion function in the destination class. (Assume 1$ = Rs. 50.00) 45. Program to convert an object of one class to object of another class & vice- versa. 46. Program to create a class vector and overload >>, <<, *. 47. Program to illustrate how to derive a class from a base class. 48. Program to implement multilevel inheritance. 49. Program to implement hierarchical inheritance.
  • 6. P a g e 6 50. Program to demonstrate multiple inheritance in which a class is derived publicly from both the base classes. 51. Program to implement multiple inheritance to calculate total revenue generated from the sale of books. 52. Program to understand hybrid inheritance, consider an example of calculating the result of a student on the basis of marks obtained in internal and external examination. 53. Program to calculate the result of the student using private inheritance. 54. Program to demonstrate the use of protected members. 55. Program to illustrate the concept of overriding member functions. 56. Program to maintain student information by creating a composite class having one of its data member as an object of another predefined class. 57. Program to illustrate how base class pointer is used in conjunction with derived class objects. 58. Program to implement run time polymorphism using virtual function.
  • 7. P a g e 7
  • 8. P a g e 8 // Program to add two numbers. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int a, b; clrscr(); cout << "Enter value of a: "; cin >> a; cout << "Enter value of b: "; cin >> b; cout << "Sum = " << (a + b); getch(); }
  • 9. P a g e 9
  • 10. P a g e 10 // Program to print first 10 natural numbers. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int i; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++) cout << i << " "; getch(); }
  • 11. P a g e 11
  • 12. P a g e 12 // Program to print sum of first 10 natural numbers. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int i, sum = 0; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++){ cout << i << " "; sum += i; } cout << "nSum = " << sum; getch(); }
  • 13. P a g e 13
  • 14. P a g e 14 // Program to check whether the entered number is prime or not. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int i, num, count = 0; clrscr(); cout << "Enter any number: "; cin >> num; for(i = 1; i <= num; i++){ if((num % i) == 0) count++; } if(count == 2) cout << num << " is a prime number."; else cout << num << " is not a prime number."; getch(); }
  • 15. P a g e 15
  • 16. P a g e 16 // Program to print factorial of a number. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i, num, fact = 1; cout << "nEnter any number: "; cin >> num; for(i = 1; i <= num; i++) fact *= i; cout << "Factorial of " << num << " = " << fact; getch(); }
  • 17. P a g e 17
  • 18. P a g e 18 // Program to print Fibonacci series. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int i, n, a = 0, b = 1, c; clrscr(); cout << "Enter no. of terms: "; cin >> n; if(n == 1) cout << "Fibonacci series: " << a << " "; else cout << "Fibonacci series: " << a << " " << b << " "; for(i = 3; i <= n; i++){ c = a + b; cout << c << " "; a = b; b = c; } getch(); }
  • 19. P a g e 19
  • 20. P a g e 20 // Program to check whether the no. is odd or even. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int num; clrscr(); cout << "Enter a number: "; cin >> num; if((num % 2) == 0) cout << num << " is an even number."; else cout << num << " is an odd number."; getch(); }
  • 21. P a g e 21
  • 22. P a g e 22 // Program to print day of the week based on the day number entered. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ int dnum; clrscr(); cout << "Enter any day number (1-7): "; cin >> dnum; switch(dnum){ case 1: cout << "Sunday"; break; case 2: cout << "Monday"; break; case 3: cout << "Tuesday"; break; case 4: cout << "Wednesday"; break; case 5: cout << "Thursday"; break; case 6: cout << "Friday"; break; case 7: cout << "Saturday"; break; default: cout << "Invalid day no."; } getch(); }
  • 23. P a g e 23
  • 24. P a g e 24 // Program to print first 10 odd natural numbers using while & do while loop. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i = 1, x = 1; cout << "While loop: " << endl; cout << "First 10 odd natural numbers: "; while(i <= 10){ cout << x << " "; x += 2; i++; } i = 1; x = 1; cout << "nnDo while loop: " << endl; cout << "First 10 odd natural numbers: "; do{ cout << x << " "; x += 2; i++; }while(i <= 10); getch(); }
  • 25. P a g e 25
  • 26. P a g e 26 // Program to move the cursor to the next line using endl manipulator. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); cout << "Enter name: " << endl; cout << "My name is Jasbir Singh."; getch(); }
  • 27. P a g e 27
  • 28. P a g e 28 // Program to enter a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int i; cout << "nEnter a hexadecimal no.: "; cin >> hex >> i; cout << "nValue in hex format = " << hex << i; cout << "nValue in octal format = " << oct << i; cout << "nValue in decimal format = " << dec << i; getch(); }
  • 29. P a g e 29
  • 30. P a g e 30 // Program to input a variable in octal form and display the variable in different number system using setbase(b). // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int num; cout << "nEnter value of num in octal form: "; cin >> setbase(8) >> num; cout << "nValue in hex form = " << setbase(16) << num; cout << "nValue in oct form = " << setbase(8) << num; cout << "nValue in dec form = " << setbase(10) << num; getch(); }
  • 31. P a g e 31
  • 32. P a g e 32 // Program to display a cout statement within the width specified. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int age = 19, rollno = 7009; cout << setw(12) << "My age is" << setw(8) << age << endl; cout << setw(12) << "My rollno is" << setw(8) << rollno; getch(); }
  • 33. P a g e 33
  • 34. P a g e 34 // Program to fill empty spaces with the specified character c using setfill() manipulator. // Written by Jasbir Singh. #include<iostream.h> #include<graphics.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int age = 19, rollno = 7009; cout << setw(4) << setfill('#') << age << setw(6) << rollno << endl; cout << setw(6) << age << setw(8) << rollno; getch(); }
  • 35. P a g e 35
  • 36. P a g e 36 // Program to round off a number to 2 and 3 decimal places. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); float a = 43.455368; cout << setprecision(2) << a << endl; cout << setprecision(3) << a; getch(); }
  • 37. P a g e 37
  • 38. P a g e 38 // Program to calculate the area of a rectangle using objects & classes. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class rectangle{ int l, b; public: void setdata(int x, int y){ l = x; b = y; } void area(){ cout << "Area = " << (l * b) << endl; } }; void main(){ clrscr(); rectangle r1, r2; cout << "nFirst rectangle " << endl; r1.setdata(8, 3); r1.area(); cout << "nSecond rectangle "; cout << "nEnter length & breadth: "; int x, y; cin >> x >> y; r2.setdata(x, y); r2.area(); getch(); }
  • 39. P a g e 39
  • 40. P a g e 40 // Program to calculate the area of a rectangle by defining member function outside the class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class rectangle{ int l, b; public: void setdata(int, int); void area(); }; void rectangle::setdata(int x, int y){ l = x; b = y; } void rectangle::area(){ cout << "Area = " << (l * b) << endl; } void main(){ clrscr(); rectangle r1, r2; cout << "nFirst rectangle " << endl; r1.setdata(8, 3); r1.area(); cout << "nSecond rectangle "; cout << "nEnter length & breadth: "; int x, y; cin >> x >> y; r2.setdata(x, y); r2.area(); getch(); }
  • 41. P a g e 41
  • 42. P a g e 42 // Program to calculate gross salary of an employee by demonstrating how the private member functions can be accessed from within the class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class employee{ char ename[30]; int bs; double cal_da(){ return (0.05 * bs); } double cal_hra(){ return (0.15 * bs); } public: void setdata(void); void total_sal(void); }; void employee::setdata(){ cout << "Enter employee name: "; cin >> ename; cout << "Enter basic salary: "; cin >> bs; } void employee::total_sal(){ double total; double da = cal_da(); double hra = cal_hra(); total = bs + da + hra; cout << "nTotal salary = " << total; } void main(){ clrscr(); employee e; e.setdata(); e.total_sal(); getch(); }
  • 43. P a g e 43
  • 44. P a g e 44 // Program to demonstrate an array of bank account objects. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class account{ int accno; char name[20]; double bal; public: void input_data(); void display_data(); }; void account::input_data(){ cout << "n-------------------------n"; cout << "Enter account no.: "; cin >> accno; cout << "Enter name: "; cin >> name; cout << "Enter balance: "; cin >> bal; } void account::display_data(){ cout << "n-------------------------"; cout << "nAccount no.= " << accno; cout << "nName = " << name; cout << "nBalance = " << bal; } void main(){ clrscr(); account a[30]; int n; cout << "nEnter the no. of persons whose info you want to add: "; cin >> n; for(int i = 0; i < n; i++){ cout << "nEnter info of " << (i + 1) << " person: "; a[i].input_data(); } for(i = 0; i < n; i++){ cout << "nPerson " << (i + 1) << " details: "; a[i].display_data(); } getch(); }
  • 45. P a g e 45
  • 46. P a g e 46 // Program to demonstrate passing an object(s) by value. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class time{ int hrs, mins; public: void read_time(); void show_time(); void add_time(time, time); }; void time::read_time(){ cout << "nEnter hours & minutes: "; cin >> hrs >> mins; } void time::show_time(){ cout << "nTime is " << hrs << ":" << mins; } void time::add_time(time t1, time t2){ int m = t1.mins + t2. mins; int h = (m/60); mins = (m%60); hrs = t1.hrs + t2.hrs + h; } void main(){ clrscr(); time t1, t2, t3; cout << "nEnter 1st time: "; t1.read_time(); cout << "nEnter 2nd time: "; t2.read_time(); t3.add_time(t1, t2); cout << "nFinal time: "; t3.show_time(); getch(); }
  • 47. P a g e 47
  • 48. P a g e 48 // Program to demonstrate passing an object(s) by reference. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class account{ int accno; float balance; public: void read(); void show(); void transfer(account &, double); }; void account::read(){ cout << "nEnter account no. = "; cin >> accno; cout << "Enter balance = "; cin >> balance; } void account::show(){ cout << "nAccount no. = " << accno; cout << "nBalance = " << balance; } void account::transfer(account &act, double amt){ balance -= amt; act.balance += amt; } void main(){ clrscr(); account a1, a2; cout << "Enter 1st account info: "; a1.read(); cout << "Enter 2nd account info: "; a2.read(); cout << "Enter amount to be transferred from account 1 to account 2: "; double money; cin >> money; a1.transfer(a2, money); cout << "nDisplaying 1st account info: "; a1.show(); cout << "nDisplaying 2nd account info: "; a2.show(); getch(); }
  • 49. P a g e 49
  • 50. P a g e 50 // Program to add two time objects by returning an object from a function. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class time{ int hrs, mins; public: void read_time(); void show_time(); time add_time(time); }; void time::read_time(){ cout << "nEnter hours & minutes: "; cin >> hrs >> mins; } void time::show_time(){ cout << "nTime is " << hrs << ":" << mins; } time time::add_time(time t){ time temp; int m = mins + t.mins; int h = (m/60); temp.mins = (m%60); temp.hrs = hrs + t.hrs + h; return temp; } void main(){ clrscr(); time t1, t2, t3; cout << "nEnter 1st time: "; t1.read_time(); cout << "nEnter 2nd time: "; t2.read_time(); t3 = t1.add_time(t2); cout << "nTime after adding: "; t3.show_time(); getch(); }
  • 51. P a g e 51
  • 52. P a g e 52 // Program to understand the concept of static data members. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class account{ int accno; double bal; static double rate; public: void read(); void show(); void qtr_rate_call(); }; void account::read(){ cout << "nEnter account no.: "; cin >> accno; cout << "Enter balance: "; cin >> bal; } void account::show(){ cout << "nAccount No. = " << accno; cout << "nBalance = " << bal; } double account::rate = 0.05; void account::qtr_rate_call(){ double interest = (bal * rate * 0.25); bal += interest; } void main(){ clrscr(); account a1, a2; cout << "nEnter 1st account info: "; a1.read(); cout << "nEnter 2nd account info: "; a2.read(); a1.qtr_rate_call(); a2.qtr_rate_call(); cout << "nAfter giving quarter interest: "; a1.show(); a2.show(); getch(); }
  • 53. P a g e 53
  • 54. P a g e 54 // Program to understand the concept of static data members. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class account{ int accno; double bal; static double rate; public: void read(); void show(); void qtr_rate_call(); }; void account::read(){ cout << "nEnter account no.: "; cin >> accno; cout << "Enter balance: "; cin >> bal; } void account::show(){ cout << "nAccount No. = " << accno; cout << "nBalance = " << bal; } double account::rate = 0.05; void account::qtr_rate_call(){ double interest = (bal * rate * 0.25); bal += interest; } void main(){ clrscr(); account a1, a2; cout << "nEnter 1st account info: "; a1.read(); cout << "nEnter 2nd account info: "; a2.read(); a1.qtr_rate_call(); a2.qtr_rate_call(); cout << "nAfter giving quarter interest: "; a1.show(); a2.show(); getch(); }
  • 55. P a g e 55
  • 56. P a g e 56 // Program to show the concept of function overloading to calculate area where same name functions differ in number of parameters. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> #include<math.h> void area(int); void area(int, int); void area(int, int, int); void main(){ clrscr(); int s, l, b, s1, s2, s3; cout << "nEnter side of square: "; cin >> s; area(s); cout << "nEnter length & breadth of a rectangle: "; cin >> l >> b; area(l, b); cout << "nEnter sides of triangle: "; cin >> s1 >> s2 >> s3; area(s1, s2, s3); getch(); } void area(int x){ cout << "Area = " << (x * x) << endl; } void area(int x, int y){ cout << "Area = " << (x * y) << endl; } void area(int x, int y, int z){ float sp = (x + y + z)/2; float ar = sqrt(sp * (sp - x) * (sp - y) * (sp - z)); cout << "Area = " << ar << endl; }
  • 57. P a g e 57
  • 58. P a g e 58 // Program to illustrate the use of constructor member function to initialize an object during its creation. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class rectangle{ int l, b; public: rectangle(){ l = 5; b = 3; } int area(){ return (l * b); } }; void main(){ clrscr(); rectangle r; cout << "nArea of rectangle = " << r.area(); getch(); }
  • 59. P a g e 59
  • 60. P a g e 60 // Program to initialize an object with different set of values using parameterized constructor. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class rectangle{ int l, b; public: rectangle(){ l = b = 0; } rectangle(int x, int y){ l = x; b = y; } int area(){ return (l * b); } }; void main(){ clrscr(); rectangle r1(6, 4), r2(9, 5); cout << "nArea of 1st rectangle = " << r1.area(); cout << "nArea of 2nd rectangle = " << r2.area(); getch(); }
  • 61. P a g e 61
  • 62. P a g e 62 // Program to understand the concept of copy constructor. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class count{ int c; public: count(){} count(int x){ c = x; } count(count &obj){ c = obj.c; cout << "nCopy constructor invoked."; } void show(){ cout << "nCount = " << c; } }; void main(){ clrscr(); count c1, c2(5), c3(c2); c1.show(); c2.show(); c3.show(); getch(); }
  • 63. P a g e 63
  • 64. P a g e 64 // Program to demonstrate the use of constructor overloading and destructor. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class rectangle{ int l, b; public: rectangle(){ l = b = 0; cout << "nDefault constuctor called."; } rectangle(int x){ l = b = x; cout << "nConstructor with one parameter called."; } rectangle(int x, int y){ l = x; b = y; cout << "nConstructor with two parameters called."; } rectangle(rectangle &ob){ l = ob.l; b = ob.b; cout << "nCopy constructor called."; } int area(){ return (l * b); } ~rectangle(){ cout << "nDestructor called."; cout << "nreleasing memory......" ; } }; void main(){ clrscr(); rectangle r1, r2(4), r3(6, 3), r4(r2); cout << "nArea of 1st rectangle = " << r1.area(); cout << "nArea of 2nd rectangle = " << r2.area(); cout << "nArea of 3rd rectangle = " << r3.area(); cout << "nArea of 4th rectangle = " << r4.area(); getch(); }
  • 65. P a g e 65
  • 66. P a g e 66 // Program to demonstrate how non-member function can access private data members of a class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class A{ int a; public: A(int x){ a = x; } friend void display(A); }; void display(A obj){ cout << "nA = " << obj.a; } void main(){ clrscr(); A ob(5); display(ob); getch(); }
  • 67. P a g e 67
  • 68. P a g e 68 // Program to demonstrate how a friend function acts as a bridge between two classes. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class B; class A{ int a; public: A(int x){ a = x; } friend void diff(A, B); }; class B{ int b; public: B(int y){ b = y; } friend void diff(A, B); }; void diff(A obj1, B obj2){ cout << "nDifference = " << (obj1.a - obj2.b); } void main(){ clrscr(); A obj1(8); B obj2(5); diff(obj1, obj2); getch(); }
  • 69. P a g e 69
  • 70. P a g e 70 // Program to illustrate that a member function of one class can act as a friend of another class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class B; class A{ int a; public: A(int x){ a = x; } void diff(B); }; class B{ int b; public: B(int y){ b = y; } friend void A::diff(B); }; void A::diff(B obj){ cout << "nDifference = " << (a - obj.b); } void main(){ clrscr(); A obj1(8); B obj2(5); obj1.diff(obj2); getch(); }
  • 71. P a g e 71
  • 72. P a g e 72 // Program to illustrate how to declare the entire class as a friend of another class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class A{ int a, b; public: A(int x, int y){ a = x; b = y; } friend class B; }; class B{ public: void display_a(A obj){ cout << "na = " << obj.a; } void display_b(A obj){ cout << "nb = " << obj.b; } }; void main(){ clrscr(); A obj1(10, 15); B obj2(); obj2.display_a(obj1); obj2.display_b(obj1); getch(); }
  • 73. P a g e 73
  • 74. P a g e 74 // Program to increment a number by 1 without overloading the operator. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class score{ int val; public: score(){ val = 0; } void increment(){ val++; } int show(){ return val; } }; void main(){ clrscr(); score s1, s2; cout << "nInitial value of s1 = " << s1.show(); cout << "nInitial value of s2 = " << s2.show(); s1.increment(); s1.increment(); s2.increment(); cout << "nFinal value of s1 = " << s1.show(); cout << "nFinal value of s2 = " << s2.show(); getch(); }
  • 75. P a g e 75
  • 76. P a g e 76 // Program to overload unary increment operator (++). // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class score{ int val; public: score(){ val = 0; } void operator++(){ val++; } int show(){ return val; } }; void main(){ clrscr(); score s1, s2; cout << "nInitial value of s1 = " << s1.show(); cout << "nInitial value of s2 = " << s2.show(); s1++; ++s1; s2++; cout << "nFinal value of s1 = " << s1.show(); cout << "nFinal value of s2 = " << s2.show(); getch(); }
  • 77. P a g e 77
  • 78. P a g e 78 // Program showing overloading of prefix and postfix increment operators. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class score{ int val; public: score(){ val = 0; } score operator++(){ score temp; val = val + 1; temp.val = val; return temp; } score operator++(int){ score temp; temp.val = val; val = val + 1; return temp; } int show(){ return val; } }; void main(){ clrscr(); score s1, s2; cout << "nInitial value of s1 = " << s1.show(); cout << "nInitial value of s2 = " << s2.show(); s2 = s1++; s1 = ++s2; s1 = s1++; cout << "nFinal value of s1 = " << s1.show(); cout << "nFinal value of s2 = " << s2.show(); getch(); }
  • 79. P a g e 79
  • 80. P a g e 80 // Program to add two complex numbers in which addition is performed by // overloading the binary operator +. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class complex{ double real, imag; public: complex(){ real = imag = 0.0; } void read(){ cout << "nEnter real & imaginary part: "; cin >> real >> imag; } complex operator+(complex obj){ complex temp; temp.real = real + obj.real; temp.imag = imag + obj.imag; return temp; } void show(); }; void complex::show(){ if(imag > 0) cout << real << "+" << imag << "i"; else if(imag == 0) cout << real; else cout << real << imag << "i"; } void main(){ clrscr(); complex c1, c2, c3; cout << "nEnter 1st complex no.: "; c1.read(); cout << "nEnter 2nd complex no.: "; c2.read(); c3 = c1 + c2; cout << "nSum of two complex numbers = "; c3.show(); getch(); }
  • 81. P a g e 81
  • 82. P a g e 82 // Program to overload a unary operator using friend function. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class score{ int val; public: score(){ val = 0; } int show(){ return val; } friend score operator++(score &); friend score operator++(score &, int); }; void main(){ clrscr(); score s1, s2; cout << "nInitial value of s1 = " << s1.show(); cout << "nInitial value of s2 = " << s2.show(); s2 = s1++; s1 = ++s2; s1 = s1++; cout << "nFinal value of s1 = " << s1.show(); cout << "nFinal value of s2 = " << s2.show(); getch(); } score operator++(score &obj){ score temp; obj.val = obj.val + 1; temp.val = obj.val; return temp; } score operator++(score &obj, int){ score temp; temp.val = obj.val; obj.val = obj.val + 1; return temp; }
  • 83. P a g e 83
  • 84. P a g e 84 // Program to compare the date of birth of two persons by overloading the equality operator (==). // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class dob{ int dd, mm, yy; public: dob(int d, int m, int y){ dd = d; mm = m; yy = y; } void show(){ cout << dd << "-" << mm << "-" << yy; } int operator==(dob); }; int dob::operator==(dob obj){ if((dd == obj.dd) && (mm == obj.mm) && (yy == obj.yy)) return 1; else return 0; } void main(){ clrscr(); dob d1(14, 5, 2018), d2(1, 1, 1980), d3(14, 5, 2018); if(d1 == d2) cout << "nTwo dates are equal - d1 & d2."; else cout << "nTwo dates are not equal."; if(d1 == d3) cout << "nTwo dates are equal - d1 & d3."; else cout << "nTwo dates are not equal."; getch(); }
  • 85. P a g e 85
  • 86. P a g e 86 // Program to add two time objects by overloading += operator. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class time{ int hrs, mins, secs; public: time(int h, int m, int s){ hrs = h; mins = m; secs = s; } void show(){ cout << "nTime is -> " << hrs << ":" << mins << ":" << secs; } void operator+=(time); }; void time::operator+=(time t){ secs += t.secs; mins += t.mins; hrs += t.hrs; if(secs >= 60){ secs -= 60; mins++; } if(mins >= 60){ mins -= 60; hrs++; } } void main(){ clrscr(); time t1(4, 50, 32), t2(6, 30, 20); t1.show(); t1 += t2; cout << "nTime after overloading += "; t1.show(); getch(); }
  • 87. P a g e 87
  • 88. P a g e 88 // Program to understand how to perform conversion of data from basic to user-defined type. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class time{ int hrs, mins; public: time(int t){ hrs = t/60; mins = t%60; } void show(){ cout << "nTime -> " << hrs << ":" << mins; } }; void main(){ clrscr(); int m = 96; time t1 = m; t1.show(); getch(); }
  • 89. P a g e 89
  • 90. P a g e 90 // Program to understand how to perform conversion of data from user- defined (class) to basic type. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class time{ int hrs, mins; public: time(int h, int m){ hrs = h; mins = m; } void show(){ cout << "nTime -> " << hrs << ":" << mins; } operator int(){ return (hrs * 60) + mins; } }; void main(){ clrscr(); time t(1, 36); int minutes = t; cout << "nTime in hrs & mins form: "; t.show(); cout << "nTime in minutes only: " << minutes; getch(); }
  • 91. P a g e 91
  • 92. P a g e 92 // Program to convert given amount in dollars to rupees. (Assume 1$ = Rs. 50.00) // Written by Jasbir Singh. #define dol_val 50.00 #include<iostream.h> #include<conio.h> class rupee{ double rs; public: rupee(){ rs = 0; } rupee(double r){ rs = r; } void show(){ cout << "nMoney in Rs. = " << rs; } }; class dollar{ double dol; public: dollar(double doll){ dol = doll; } void show(){ cout << "nMoney in $ = " << dol; } operator rupee(){ return (rupee (dol * dol_val)); } }; void main(){ clrscr(); system("color f8"); dollar d(3); d.show(); rupee r; r = d; r.show(); getch(); }
  • 93. P a g e 93
  • 94. P a g e 94 // Program to convert given amount in dollars to rupees by defining conversion function in the destination class. (Assume 1$ = Rs. 50.00) // Written by Jasbir Singh #include<iostream.h> #include<conio.h> #define dol_val 50.00 class dollar{ double dol; public: dollar(double doll){ dol = doll; } void show(){ cout << "nMoney in dollars = " << dol; } double ret_dol(){ return dol; } }; class rupee{ double rs; public: rupee(){} rupee(dollar d){ rs = d.ret_dol() * dol_val; } void show(){ cout << "nMoney in rupees = " << rs; } }; void main(){ clrscr(); dollar d(3); rupee r; d.show(); r = d; r.show(); getch(); }
  • 95. P a g e 95
  • 96. P a g e 96 // Program to convert an object of one class to object of another class & vice-versa. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> #define dol_val 50.00 class rupee{ double rs; public: rupee(){} rupee(double r){ rs = r; } void show(){ cout << "nMoney in rupees = " << rs; } double ret_rs(){ return rs; } }; class dollar{ double dol; public: dollar(){} dollar(double doll){ dol = doll; } dollar(rupee r){ dol = r.ret_rs() / dol_val; } operator rupee(){ return(rupee (dol * dol_val)); } void show(){ cout << "nMoney in dollars = " << dol; } }; void main(){ clrscr(); dollar d1(3), d2; rupee r1(150), r2; d1.show(); r2 = d1;
  • 97. P a g e 97 r2.show(); r1.show(); d2 = r1; d2.show(); getch(); }
  • 98. P a g e 98
  • 99. P a g e 99 // Program to create a class vector and overload >>, <<, *. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> const int size = 5; class vector{ int v[size]; public: vector(); vector(int *); vector operator*(int); friend vector operator*(int, vector); 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 vector::operator*(int j){ vector temp; for(int i = 0; i < size; i++) temp.v[i] = v[i] * j; return temp; } vector operator*(int j, vector a){ vector temp; for(int i = 0; i < size; i++) temp.v[i] = j * a.v[i]; return temp; } istream & operator>>(istream &din, vector &a){ for(int i = 0; i < size; i++) din >> a.v[i]; return din; } ostream & operator<<(ostream &dout, vector &a){ dout << "(" << a.v[0]; for(int i = 1; i < size; i++)
  • 100. P a g e 100 dout << ", " << a.v[i]; dout << ")"; } void main(){ clrscr(); vector v1; int x[] = {1, 2, 3, 4, 5}; vector v2(x); v1 = 2 * v2; vector v3; cout << "nEnter vector: "; cin >> v3; cout << "nVector entered"; cout << "nVector v1 after multiplication: " << v1; cout << "nVector v2: " << v2; cout << "nVector v3: " << v3; getch(); }
  • 101. P a g e 101
  • 102. P a g e 102 // Program to illustrate how to derive a class from a base class. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> class base_class{ int num1; public: void base_read(){ cout << "nEnter the value for num1: "; cin >> num1; } void base_show(){ cout << "nNum1 = " << num1; } }; class der_class: public base_class{ int num2; public: void der_read(){ cout << "nEnter the value for num2: "; cin >> num2; } void der_show(){ cout << "nNum2 = " << num2; } }; void main(){ clrscr(); der_class d; d.base_read(); d.der_read(); d.base_show(); d.der_show(); getch(); }
  • 103. P a g e 103
  • 104. P a g e 104 // Program to implement multilevel inheritance. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class person{ char name[20]; long int phno; public: void read(){ cout << "nEnter name & phone no.: "; cin >> name >> phno; } void show(){ cout << "nName = " << name; cout << "nPhone No. = " << phno; } }; class student: public person{ int rollno; char course[20]; public: void read(){ person::read(); cout << "nEnter roll no. & course: "; cin >> rollno >> course; } void show(){ person::show(); cout << "nRoll No. = " << rollno; cout << "nCourse = " << course; } }; class exam: public student{ int m[4]; double per; public: void read(); void show(); void cal(); }; void exam::read(){ student::read(); cout << "nEnter marks: ";
  • 105. P a g e 105 for(int i = 0; i < 4; i++) cin >> m[i]; } void exam::show(){ student::show(); cout << "nMarks are: "; for(int i = 0; i < 4; i++) cout << m[i] << "t"; cout << "nPercantage = " << per; } void exam::cal(){ int tm = 0; for(int i = 0; i < 4; i++) tm += m[i]; per = double (tm) / 4; } void main(){ clrscr(); exam e; e.read(); e.cal(); e.show(); getch(); }
  • 106. P a g e 106
  • 107. P a g e 107 // Program to implement hierarchical inheritance. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class person{ char name[20]; long int phno; public: void read(){ cout << "nEnter name & phone no.: "; cin >> name >> phno; } void show(){ cout << "nName = " << name; cout << "nPhone No. = " << phno; } }; class student: public person{ int rollno; char course[20]; public: void read(){ person::read(); cout << "nEnter roll no. & course: "; cin >> rollno >> course; } void show(){ person::show(); cout << "nRoll No. = " << rollno; cout << "nCourse = " << course; } }; class teacher: public person{ char dept[20]; char qual[10]; public: void read(){ person::read(); cout << "nEnter department & qualification: "; cin >> dept >> qual; } void show(){ person::show();
  • 108. P a g e 108 cout << "nDepartment = " << dept; cout << "nQualification = " << qual; } }; void main(){ clrscr(); student s; teacher t; cout << "nEnter student's information: "; s.read(); cout << "nEnter teacher's information: "; t.read(); cout << "nDisplaying student's details: "; s.show(); cout << "nDisplaying teacher's details: "; t.show(); getch(); }
  • 109. P a g e 109
  • 110. P a g e 110 // Program to demonstrate multiple inheritance in which a class is derived publicly from both the base classes. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> class base1{ protected: int x; public: void readx(){ cout << "nEnter the value of x: "; cin >> x; } void showx(){ cout << "nx = " << x; } }; class base2{ protected: int y; public: void ready(){ cout << "nEnter the value of y: "; cin >> y; } void showy(){ cout << "ny = " << y; } }; class der: public base1, public base2{ int z; public: void cal(){ z = x + y; } void showz(){ cout << "nz = " << z; } }; void main(){ clrscr(); der d; d.readx();
  • 111. P a g e 111 d.ready(); d.cal(); d.showx(); d.showy(); d.showz(); getch(); }
  • 112. P a g e 112
  • 113. P a g e 113 // Program to implement multiple inheritance to calculate total revenue generated from the sale of books. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> class publication{ char publisher[20]; protected: double price; public: void read(){ cout << "nEnter publisher & price: "; cin >> publisher >> price; } void show(){ cout << "nPublisher = " << publisher; cout << "nPrice = " << price; } }; class sales{ protected: double pub_sales; public: void read(){ cout << "nEnter total sales: "; cin >> pub_sales; } void show(){ cout << "nTotal sales = " << pub_sales; } }; class book: public publication, public sales{ char author[20]; int pages; long isbn; public: void read(){ cout << "nEnter author, no. of pages, isbn num: "; cin >> author >> pages >> isbn; } void show(){ cout << "nAuthor's name = " << author; cout << "nNo. of pages = " << pages;
  • 114. P a g e 114 cout << "nISBN number = " << isbn; } void cal_salamt(){ double amt = price * pub_sales; cout << "nSales Amount = " << amt; } }; void main(){ clrscr(); book b; cout << "nEnter publication details: "; b.publication::read(); cout << "nEnter sales details: "; b.sales::read(); cout << "nEnter book details: "; b.read(); b.cal_salamt(); cout << "nDetails are: "; b.publication::show(); b.sales::show(); b.show(); getch(); }
  • 115. P a g e 115
  • 116. P a g e 116 // Program to understand hybrid inheritance, consider an example of calculating the result of a student on the basis of marks obtained in internal and external examination. // Written by Jasbir Singh #include<iostream.h> #include<conio.h> class student{ int rollno; char name[20]; public: void read(){ cout << "nEnter rollno & name: "; cin >> rollno >> name; } void show(){ cout << "nRoll no = " << rollno; cout << "nName = " << name; } }; class internal: public student{ protected: int sub1, sub2; public: void read_marks(){ cout << "nEnter internal marks of subject 1 (MAX 40): "; cin >> sub1; cout << "nEnter internal marks of subject 2 (MAX 40): "; cin >> sub2; } void display_marks(){ cout << "nInternal marks of subject 1 = " << sub1; cout << "nInternal marks of subject 2 = " << sub2; } }; class external{ protected: int sub1, sub2; public: void read_marks(){ cout << "nEnter external marks of subject 1 (MAX 60): "; cin >> sub1; cout << "nEnter external marks of subject 2 (MAX 60): "; cin >> sub2;
  • 117. P a g e 117 } void display_marks(){ cout << "nExternal marks of subject 1 = " << sub1; cout << "nExternal marks of subject 2 = " << sub2; } }; class result: public internal, public external{ int total_marks; public: void cal_result(){ total_marks = internal::sub1 + internal::sub2 + external::sub1 + external::sub2; cout << "nTotal marks obtained = " << total_marks; } }; void main(){ clrscr(); result r1; cout << "nEnter student information: "; r1.read(); cout << "nEnter marks of internal examination: "; r1.internal::read_marks(); cout << "nEnter marks of external examination: "; r1.external::read_marks(); cout << "nDisplaying student details: "; r1.show(); r1.internal::display_marks(); r1.external::display_marks(); cout << "nCalculating & displaying result: "; r1.cal_result(); getch(); }
  • 118. P a g e 118
  • 119. P a g e 119 // Program to calculate the result of the student using private inheritance. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class stud_base{ int rollno; char name[20]; public: void read(){ cout << "nEnter roll no. & name: "; cin >> rollno >> name; } void show(){ cout << "nRoll no. = " << rollno; cout << "nName = " << name; } }; class result: private stud_base{ int m[4]; double per; public: void input(); void cal(); void display(); }; void result::input(){ read(); cout << "nEnter marks: n"; for(int i = 0; i < 4; i++) cin >> m[i]; } void result::cal(){ int total = 0; for(int i = 0; i < 4; i++) total += m[i]; per = double (total)/ 4; } void result::display(){ show(); cout << "nMarks are: n"; for(int i = 0; i < 4; i++) cout << m[i] << endl; cout << "nPercentage = " << per;
  • 120. P a g e 120 } void main(){ clrscr(); result r; clrscr(); r.input(); r.cal(); r.display(); getch(); }
  • 121. P a g e 121
  • 122. P a g e 122 // Program to demonstrate the use of protected members. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class num{ protected: int x, y; public: void read(){ cout << "nEnter two numbers: "; cin >> x >> y; } void show(){ cout << "nx = " << x; cout << "ny = " << y; } }; class result: public num{ int z; public: void add(){ z = x + y; } void showz(){ cout << "nz = " << z; } }; void main(){ clrscr(); result r; r.read(); r.add(); r.show(); r.showz(); getch(); }
  • 123. P a g e 123
  • 124. P a g e 124 // Program to illustrate the concept of overriding member functions. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class person{ long int phno; char name[20]; public: void read(){ cout << "nEnter name & phone no.: "; cin >> name >> phno; } void show(){ cout << "nName = " << name; cout << "nPhone no. = " << phno; } }; class student: public person{ int rollno; char course[20]; public: void read(){ person::read(); cout << "nEnter rollno. & course: "; cin >> rollno >> course; } void show(){ person::show(); cout << "nRoll no. = " << rollno; cout << "nCourse = " << course; } }; void main(){ clrscr(); student s; s.read(); s.show(); getch(); }
  • 125. P a g e 125
  • 126. P a g e 126 // Program to maintain student information by creating a composite class having one of its data member as an object of another predefined class. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class student{ int rollno; char name[20]; class dob{ int dd, mm, yy; public: void read(){ cout << "nEnter date, month & year: "; cin >> dd >> mm >> yy; } void show(){ cout << "nDate = " << dd << "/" << mm << "/" << yy; } }d; public: void read(){ cout << "nEnter roll no. & name: "; cin >> rollno >> name; d.read(); } void show(){ cout << "nRoll no. = " << rollno; cout << "nName = " << name; d.show(); } }; void main(){ clrscr(); student s; s.read(); s.show(); getch(); }
  • 127. P a g e 127
  • 128. P a g e 128 // Program to illustrate how base class pointer is used in conjunction with derived class objects. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class base{ public: void display(){ cout << "nBase class display called."; } }; class der1: public base{ public: void display(){ cout << "nDer1's display called."; } }; class der2: public base{ public: void display(){ cout << "nDer2's display called."; } }; void main(){ clrscr(); base *ptr; der1 d1; der2 d2; ptr->display(); ptr = &d1; ptr->display(); ptr = &d2; ptr->display(); getch(); }
  • 129. P a g e 129
  • 130. P a g e 130 // Program to implement run time polymorphism using virtual function. // Written by Jasbir Singh. #include<iostream.h> #include<conio.h> class base{ public: virtual void display(){ cout << "nBase class display called."; } }; class der1: public base{ public: void display(){ cout << "nDer1's display called."; } }; class der2: public base{ public: void display(){ cout << "nDer2's display called."; } }; void main(){ clrscr(); base *ptr; der1 d1; der2 d2; ptr = &d1; ptr->display(); ptr = &d2; ptr->display(); getch(); }