SlideShare una empresa de Scribd logo
1 de 130
Descargar para leer sin conexión
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();
}

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Data types in C
Data types in CData types in C
Data types in C
 
Oops practical file
Oops practical fileOops practical file
Oops practical file
 
C tokens
C tokensC tokens
C tokens
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
C++ file
C++ fileC++ file
C++ file
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Function in c
Function in cFunction in c
Function in c
 
Strings in C
Strings in CStrings in C
Strings in C
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Built in function
Built in functionBuilt in function
Built in function
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
user defined function
user defined functionuser defined function
user defined function
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 

Similar a Object Oriented Programming Using C++ Practical File

programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ reportvikram mahendra
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answersQuratulain Naqvi
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQLvikram mahendra
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfayush616992
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18HIMANSHU .
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentalsZaibi Gondal
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 

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

C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ report
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdf
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
COMPUTER SCIENCE INVESTIGATORY PROJECT 2017-18
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Bijender (1)
Bijender (1)Bijender (1)
Bijender (1)
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
Computer Practical XII
Computer Practical XIIComputer Practical XII
Computer Practical XII
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 

Último

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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(); }