Web & Social Media Analytics Previous Year Question Paper.pdf
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.
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}