CPP Pgdca
CPP Pgdca
a)
1
12
12 3
1234
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int total_rows = 4;
for(int row = 1; row<=total_rows; row++) //Dynamic initilization of int row
{
for(int col=1; col<=row; col++)
{
cout<<col;
}
cout<<endl;
}
getch();
return 0;
}
OUTPUT:
Page 1
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
b)
*
* *
* * *
* * * *
PROGRAM :
OUTPUT :
Page 2
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q2. WAP in C++ which uses functions to swap two integer & two float numbers by using
reference variable.
PROGRAM:
#include<iostream>
#include<conio.h>
using namespace std;
//function declarations
int main()
{
int a1 = 34;
int a2 = 20;
float f1 = 30.01;
float f2 = 24.40;
Page 3
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT:
Page 4
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q3. Create a single program to perform following tasks without using library functions:
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
int string_length(char);
void reverse_string(char);
//Function declearation
int main()
{
char str[20];
cout<<"Enter a string : ";
cin>>str;
cout<<endl;
int len = string_length(str);
//storing length of string
Page 5
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
reverse_string(str,len);
//passing length and string to the function
getch();
return 0;
}
OUTPUT:
Page 6
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q4. WAP in C++ to create a structure named complex having data member real and imag.
Create member function add_complex which takes structure as an argument and return
structure. Using function add two complex numbers.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
complex comp1,comp2,add_comp;
comp2.get_data();
add_comp = add(comp1,comp2);
//calling the function add and passing comp1 and comp2
cout<<endl<<"Addition of : ";
comp1.display();
cout<<" and ";
comp2.display();
cout<<" is : ";
add_comp.display();
getch();
return 0;
}
OUTPUT :
Page 8
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float n1,n2;
cout<<"Enter two values to perform arithmetic operations : ";
cin>>n1>>n2;
cout<<endl;
add(n1,n2);
subtract(n1,n2);
multi(n1,n2);
divide(n1,n2);
//inline function calling
getch();
return 0;
}
Page 9
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 10
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q6. WAP in C++ to calclulate the area of circle, reractangle,square and triangle using
inline function.
PROGRAM :
//Program to calculate the area of circle ,rectangle ,square and tringle using inline
function
#include<iostream>
#include<conio.h>
float radius;
cin>>radius;
float a,b;
cin>>a>>b;
float a;
Page 11
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cin>>a;
float height,length;
cin>>height>>length;
int main()
cout<<endl;
cout<<endl;
cout<<endl;
getch();
return 0;
Page 12
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 13
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q7. WAP in c++ To count no. of vowels, consonants in each word of a sentence passed as
argument in form of character array.
PROGRAM :
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
if(lowercase || upercase )
count_vowel++;
else if(conso_check)
count_conso++;
}
int main()
{
char str[20];
cout<<"Enter a string : ";
cin>>str;
count_vowel_conso(str);
Page 14
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 15
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q8. Write program in C++ to calculate simple interest and compound interest using default
argument.
PROGRAM :
//Program to calculate simple interest and compound interest using default argument
#include<iostream>
#include<conio.h>
using namespace std;
float simple_interest(float,float,float);
void compound_interest(float,float);
//function declaration
float simple_interest(float p,float t, float r = 3) //Default argument r (rate) is 3%
{
float Si = (p * t * r ) / 100;
cout<<"Simple Interest of principle amount ("<<p<<"), time ("<<t<<") and rate("<<r<<") is :
"<<Si<<endl<<endl;
return Si;
}
void compound_interest(float p,float si)
{
float ci = p + si;
cout<<"Compound Interest is : "<<ci<<endl;
}
int main()
{
float princ,time,rate;
cout<<endl<<endl;
cout<<"With default rate ..."<<endl;
cout<<"Enter principle amount : ";
cin>>princ;
cout<<"Enter Time (in months) : ";
cin>>time;
simp_int = simple_interest(princ,time);
// no. of actual argument is less than
//formal argument( so default argument will be passed)
compound_interest(princ,simp_int);
getch();
return 0;
}
OUTPUT :
Page 17
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q9. Create a class named calculate that uses overloaded function calclulate_area of circle,
reractangle,square and triangle.
PROGRAM :
#include<iostream>
#include<conio.h>
class calculate
float area;
public:
};
Page 18
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
if(length == width)
else
int main()
calculate obj;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
Page 19
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
getch();
return 0; }
OUTPUT :
Page 20
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q10. Create a class Student having data members to store roll number, name of student,
name of three subjects, max marks, min marks, obtained marks. Declared an object of
class student. Provide facilities to input data in data members and display result of
student?
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class student
{ int rollno;
char stu_name[20];
char sub_name[3][20];
float max_mark[3],min_mark[3],obt_mark[3];
//data members
public:
void input_data();
void result();
//member function declaration
};
Page 21
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
void student::result()
{
cout<<"Result of student "<<stu_name<<" is : "<<endl<<endl;
float total_max,total_obt;
float per;
int i;
for(i=0; i<3; i++)
{
total_max = total_max + max_mark[i];
total_obt = total_obt + obt_mark[i];
}
int main()
{
student stu1; //Object created of type student
stu1.input_data();
stu1.result();
//calling member function through object
getch();
return 0;
}
Page 22
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 23
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q11. Create a class student having data members to store rollno.,name of student, name of
3 subjects , max marks,min marks,obtain marks .use nesting of member function Declare
an array of object to input data of 3 students. Provide facilities to display result of all
students and to display result of specific student whose roll number is given ?
PROGRAM:
//Programn to store data of 3 students and display result of all and also display result of
specific student
#include<iostream>
#include<conio.h>
class student
{ int rollno;
char stu_name[20];
char sub_name[3][20];
float max_mark[3],min_mark[3],obt_mark[3];
//data members
public:
void input_data();
void disp_spec(int);
void disp_result();
};
Page 24
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cin>>rollno;
cout<<"Name : ";
cin>>stu_name;
int i;
cin>>sub_name[i];
cin>>max_mark[i];
cin>>min_mark[i];
cin>>obt_mark[i];
{ if(roll==rollno)
float total_max=0,total_obt=0;
int i;
float per;
cout<<"Percentage is = "<<per<<"%"<<endl;
if(per>=70)
cout<<"First Division.."<<endl;
cout<<"Second division.."<<endl;
cout<<"Third division..."<<endl;
else
cout<<"Fail.."<<endl;
cout<<endl; }
int main()
int i;
cout<<endl;
cout<<endl;
int roll;
cin>>roll;
getch();
return 0; }
OUTPUT:
Page 27
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Page 28
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q12. Create a class named ‘array’ having an array of integers having 5 elements as data
member provide following facilities :
Constructor to get number in array elements.
Page 29
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
class array
public:
{ int i;
a[i] = b[i]; }
void sort();
};
{ int i,j;
cout<<a[i]<<endl; }
{ int temp=a;
a=b;
b=temp; }
int main()
{ int ary[5],i;
cin>>ary[i];
getch();
return 0; }
OUTPUT :
Q13. Create a class Static_demo with static member functions for following tasks:-
1. To find factorial by recursive member function
Page 31
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
//Program to find factorial and check prime number with static member function
#include<iostream>
#include<conio.h>
using namespace std;
class Static_demo
{
public:
static double find_facto(double); //static member function declaration
static void check_prime(int); //static member function declaration
};
double Static_demo::find_facto(double n) //static member function definition
{
if( n < 1)
return 1;
else
return n*find_facto(n-1); //recursion
}
void Static_demo::check_prime(int a) //static member function definition
{
int i,j;
int c=0;
int n;
cout<<"Enter a numbet to check whether it is prime or not : ";
cin>>n;
Static_demo::check_prime(n);
//calling static member function
getch();
return 0;
}
OUTPUT :
Q14. Write a class complex having data members to store real and imaginary part provide
following
Page 33
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
class complex
{ public:
};
cout<<"Real = ";
cin>>real;
cout<<"Imaginary = ";
cin>>imag; }
{ complex temp;
Page 34
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
return temp; }
{ complex temp;
return temp; }
int main()
{ complex comp1,comp2,add,sub;
comp1.get_data();
comp2.get_data();
getch();
return 0; }
OUTPUT :
Q15. Write swapping program to demonstrate call by value , call by address and call by
reference in a single program ?
Page 35
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
//Swapping program to demonstrate call by value, call by reference and call by address
#include<iostream>
#include<conio.h>
void swap_by_call(int,int);
// function prototyping
int temp = a;
a = b;
b = temp;
int temp = a;
a = b;
b = temp;
}
Page 36
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
*a = *b;
*b = temp;
int main()
int x,y;
cin>>x>>y;
cout<<endl;
getch();
return 0;
OUTPUT :
Q16. Write a program for to create class polar data member radius and angle define
constructor of all three types and create destructor and test function in main.
Page 38
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
float radius;
float angle;
public:
{ radius = 0;
angle = 0;
{ radius = r;
angle = a;
}
Page 39
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
{ radius = p.radius;
angle = p.angle;
cout<<"Destructor Invoked"<<endl;
};
int main()
polar p2(5,7);
getch();
return 0;
OUTPUT :
Q17. WAP to create a class employee having data member employed id,salary.proide
member function for data input,output,use pointer to an object information of employee
and test the program in function main ?
Page 41
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class employee //class definition
{
int emp_id;
float salary;
//data members
public: //public area
void get_emp_data(); //member function declaration
void disp_emp_data(); //member function declaration
};
int main()
{
employee emp1;
//object emp1 created of employee class
cout<<"Accessing through object : "<<endl;
emp1.get_emp_data();
//data entered with the help of object emp1
employee *emp_ptr;
//creating pointer emp_ptr of type employee
emp_ptr = &emp1; //emp_ptr holds address of object emp1
//emp_ptr is pointer to object(emp1)
cout<<"Accessing through pointer to an object : "<<endl;
Page 42
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
emp_ptr->disp_emp_data();
//Displaying data stored in employee class via emp_ptr (pointer to an object)
getch();
return 0;
}
OUTPUT :
PROGRAM :
#include<iostream>
#include<conio.h>
#include<string.h>
class books
int book_id;
char title[20];
char author[20];
float price;
public:
void add_book();
int search_book(char);
};
Page 44
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cout<<"Book ID : ";
cin>>book_id;
cout<<"Title : ";
cin>>title;
cout<<"Author : ";
cin>>author;
cout<<"Price : ";
cin>>price;
cout<<"Edition : ";
cin>>edition;
if(strcmp(tmp_author,author)
{ display_books();
return 1; }
else
return 0; }
{ cout<<"Books deatails....."<<endl<<endl;
cout<<"Book ID : "<<book_id;
cout<<"Author : "<<author;
cout<<"Edition : "<<edition<<endl; }
void loop(char c)
{ int j;
char ch;
ch = c;
cout<<ch; }
int main()
{ books *ptr,book[20];
ptr = book;
int inc;
int total_books=0;
int k;
do{
int op;
cout<<endl;
cin>>op;
cout<<endl;
inc=0;
switch(op)
Page 46
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
{ book[total_books].add_book();
inc++;
break;
string tmp_auth;
cin>>tmp_auth;
int i;
int found_count=0;
if(found_count > 0)
else
break;
{ loop('-');
Page 47
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cout<<"Book "<<k+1<<endl;
book[k].display_books();
loop('-');
cout<<endl;
break;
case 4:
break;
default:
getch();
return 0;
OUTPUT :
Page 48
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Adding books :
Q19. Define structure student. Structure student has data members for storing name,
rollno, name of three subjects and marks. Write member function to store and print data.
Page 50
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
struct student
{
char name[20];
int rollno;
char sub_name[3][20];
float max_marks[3],min_marks[3],obt_marks[3];
//Data members of structure
void getdata()
{
cout<<"Enter name of student and roll no : ";
cin>>name>>rollno;
int i;
for(i=0; i<3; i++)
{
cout<<"Enter subject no "<<i+1<<" name : ";
cin>>sub_name[i];
cout<<"Enter Maximum marks ,Minimum marks and Obtained marks : ";
cin>>max_marks[i]>>min_marks[i]>>obt_marks[i];
}
}
void disp_data()
{
cout<<"Name of student : "<<name<<endl;
cout<<"Roll : "<<rollno<<endl;
int i;
for(i=0; i<3; i++)
{ cout<<endl;
cout<<"Name of subject "<<i+1<<" : "<<sub_name[i]<<endl;
cout<<"Maximum marks : "<<max_marks[i]<<endl;
cout<<"Minimum marks : "<<min_marks[i]<<endl;
cout<<"Obtained marks : "<<obt_marks[i]<<endl;
}
}
//member function definitions of structure
};
int main()
{
Page 51
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
return 0;
}
OUTPUT :
Q20. Write program to create a class Polar which has data member radius and angle,
define overloaded constructor to initialize object and copy constructor to initialize one
Page 52
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
class Polar
float radius;
float angle;
public:
radius = 0;
angle = 0;
this->radius = radius;
radius = p.radius;
angle = p.angle;
//contructor overloaded
void display()
cout<<"Radius : "<<radius<<endl;
cout<<"Angle : "<<angle<<endl;
};
int main()
Polar p1;
p1.display();
Polar p2(4,5);
p2.display();
Polar p3 = p2;
p3.display();
getch();
return 0;
Page 54
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Q21. Write program to create a class Polar which has data member radius and angle, use
Page 55
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
constructor with default arguments to avoid constructor overloading and copy constructor
to initialize one object by another existing object keep name of parameter of parameterized
constructor same as data members. Test functioning of the program in main function.
PROGRAM :
#include<iostream>
#include<conio.h>
class Polar
{ float radius;
float angle;
public:
this->radius = radius;
{ radius = p.radius;
angle = p.angle;
void display()
{ cout<<"Radius : "<<radius<<endl;
Page 56
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cout<<"Angle : "<<angle<<endl; }
};
int main()
p1.display();
p2.display();
p3.display();
p3.display();
getch();
return 0;
OUTPUT :
Page 57
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q22. Write a class ArraySort that uses static overloaded function to sort an array of floats,
an array of integers.
PROGRAM :
//Program to sort array of float and int using static overloaded function
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class ArraySort
{
public:
static void sort_array(float f[5]);
static void sort_array(int f[5]); //static member function overloading
static void swap(float &a,float &b);
static void swap(int &a,int &b);
};
void ArraySort :: sort_array(float f[5]) //static member function definition
{
int i,j;
for(i=0; i<5-1; i++)
{
for(j=0; j<5-1; j++)
{
if(f[j] > f[j+1])
swap(f[j],f[j+1]); //nesting static member function
}
}
cout<<"sorted float array is : "<<endl;
for(i=0; i<5; i++)
cout<<f[i]<<endl;
}
Void ArraySort :: sort_array(int f[5]) //static member function overloading
{
int i,j;
for(i=0; i<5-1; i++)
{
for(j=0; j<5-1; j++)
{
a1.sort_array(fary);
// static member function calling and passing float array
//sort_array(float) invoked
int ary[5];
cout<<"Enter Integer array : ";
for(i=0; i<5; i++)
cin>>ary[i];
a1.sort_array(ary);
// static member function calling and passing int array
//sort_array(int) invoked
getch();
return 0;
}
Page 59
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 60
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q23. Create a class Counter having a static data member, which keeps track of no. of
objects created of type Counter. One static member function must be created to increase
value of static data member as the object is created. One static member function must be
created to decrease value of static data member as the object is destroyed. One static
member function must be created to display the current value of static data member. Use
main function to test the class Counter.
PROGRAM :
//Program to make a class counter having static member functions to keep track count of
object (current,when created,when destroyed)
#include<iostream>
#include<conio.h>
using namespace std;
class counter
{
static int count_obj;
public:
counter() // default constructor
{
cout<<endl<<"..........Ojject created"<<endl;
inc_count();
//nested static member function calling
}
~counter() //destructor
{
cout<<endl<<"Object destroyed........"<<endl;
dec_count();
//nested static member function calling
}
static void inc_count()
{
count_obj++;
cout<<"Value of count is : "<<count_obj<<endl;
}
static void dec_count()
{
count_obj--;
cout<<"Value of count is : "<<count_obj<<endl;
}
static void cur_count()
{
cout<<"Current value of count is : "<<count_obj<<endl;
}
//static member functions definition
Page 61
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
};
int counter::count_obj;
int main()
{ counter c1;
//object c1 created and constructor invoked (count = 1)
{
cout<<endl<<"Block start-----> "<<endl;
counter c2;
//object c2 created and constructor invoked (count = 2)
counter::cur_count(); //current value (count =2)
//calling static member function
cout<<endl<<"<------Block ends"<<endl<<endl;
}
//object c2 destroyed and destrutor invoked (count = 1)
counter c3;
//object c3 created and construtor invoked(count=2)
OUTPUT :
Page 62
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q24. Create a class student. The student class has data members such as roll number, name
of student,contact number and address .create the derived class test which contains data
members reperesenting name of subject, and test marks of 5 subjects. Display all the
information of student.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int rollno;
char name[20];
char contact_no[20];
char addr[20];
//Data members
public:
void get_student_data();
void display_student_data();
//member function declaration
};
class test:public student //derived class test from base calss student
{
char sub_name[5][20];
float marks[5];
//Data members
public:
void get_test_data();
void display_test_data();
//member function declaration
};
void student::get_student_data() //student class member function definition
{ cout<<endl;
cout<<"Enter student details"<<endl;
cout<<"Roll no = ";
cin>>rollno;
cout<<"Name = ";
cin>>name;
cout<<"Contact no = ";
cin>>contact_no;
cout<<"Address = ";
Page 63
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cin>>addr;
}
void student::display_student_data() //student class member function definition
{
cout<<endl<<"Details of student... "<<endl<<endl;
cout<<"Roll no = "<<rollno<<"\t\t\t Name = "<<name<<endl;
cout<<"Contact no = "<<contact_no<<"\t\t\t Address = "<<addr<<endl;
}
void test::get_test_data() //test class member function definition
{
int i;
for(i=0; i<5; i++)
{
cout<<"Enter subject "<<i+1<<" name and Test marks : ";
cin>>sub_name[i]>>marks[i];
}
}
void test::display_test_data() //test class member function definition
{
int i;
cout<<endl<<"Test marks are :"<<endl<<endl;
for(i=0; i<5; i++)
{
cout<<"Subject "<<i+1<<endl;
cout<<"name = "<<sub_name[i]<<endl;
cout<<"Marks = "<<marks[i]<<endl;
cout<<endl;
}
}
int main()
{
test t1; // Crteating object of derived class
t1.get_student_data(); //calling base class member function
t1.get_test_data(); //own member function calling
Page 64
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 65
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q25. Write a program in c++ for multiple inheritance using book as derived class having
different base classes Journals,Magzines,Newpaper.
PROGRAM :
#include<iostream>
#include<conio.h>
{ char journal_name[20];
float price;
public:
void get_data()
cin>>journal_name;
cin>>price;
void disp_data()
cout<<"price : "<<price<<endl;
};
Page 66
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
char magz_name[20];
float price;
public:
void get_data()
cin>>magz_name;
cin>>price;
void disp_data()
cout<<"price : "<<price<<endl;
};
{ char news_name[20];
float price;
public:
void get_data()
cin>>news_name;
cin>>price;
void disp_data()
cout<<"price : "<<price<<endl;
};
//multiple inheritence
char book_what[20];
public:
void booking_what()
cin>>book_what;
void get_book_data()
if(book_what == "Journals")
void display_booked()
if(book_what == "Journals")
};
int main()
char op;
do{
obj.booking_what();
obj.get_book_data();
cin>>op;
Page 69
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
getch();
return 0;
OUTPUT :
Page 70
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
//Program to demostrate three classes(student , exam and result ) which is in multiple
inheritance
#include<iostream>
#include<conio.h>
using namespace std;
class student //base class 1
{
int rollno;
char name[20]; //Data members
public:
void get_student_data()
{ cout<<"Enter roll no and name of student : ";
cin>>rollno>>name;
}
void show_student_data()
{
cout<<"Roll no = "<<rollno<<"\t Name = "<<name<<endl;
}
//Member function declaration
};
class exam // base class 2
{
protected:
char sub_name[3][20];
float min_marks[3],max_marks[3],obt_marks[3];
//Data members in protected mode
public:
void get_exam_data();
void display_exam_data();
//Member function declaration
};
class result:public student,public exam //multiple inheritence
{
float total_max;
float total_obt,per;
//Data members
Page 71
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
public:
result() //Default constructor
{ total_max = 0;
total_obt = 0;
per = 0; }
void get_result();
void display_result();
//Member function declaration
};
void exam::get_exam_data() //Member function definition of exam
{ int i;
for(i=0; i<3; i++)
{
cout<<"Enter subject "<<i+1<<" name = ";
cin>>sub_name[i];
cout<<"Minimum marks = ";
cin>>min_marks[i];
cout<<"Maximum marks = ";
cin>>max_marks[i];
cout<<"Obtained marks = ";
cin>>obt_marks[i];
}
}
void exam:: display_exam_data() //Member function definition of exam
{
int i;
for(i=0; i<3; i++)
{
cout<<"Subject "<<i+1<<" name = "<<sub_name[i]<<endl;
cout<<"Minimum marks = "<<min_marks[i]<<endl;
cout<<"Maximum marks = "<<max_marks[i]<<endl;
cout<<"Obtained marks = "<<obt_marks[i]<<endl;
}
}
void result::get_result() //Member function definition of result
{
int i;
for(i=0; i<3; i++)
{
total_max = total_max + max_marks[i];
total_obt = total_obt + obt_marks[i];
}
per = total_obt * 100 / total_max;
}
void result::display_result() //Member function definition of result
{
Page 72
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 73
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q27. WAP to generate fibbonacci series use the concept of function overriding.
PROGRAM :
#include<iostream>
#include<conio.h>
{ public:
};
{ int n1=0,n2=1,n3;
int i;
cout<<"Series is : "<<endl<<endl;
cout<<n1<<" "<<n2;
{ n3=n1+n2;
cout<<" "<<n3;
n1 = n2;
n2 = n3; }
Page 74
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
public:
{ if(n==0 || n==1)
return n;
else
return fibbo(n-1)+fibbo(n-2);
};
int main()
int n;
cin>>n;
int i=0;
cout<<endl<<endl;
cin>>n;
cout<<"Series is :"<<endl<<endl;
while(i<n){
i++;
getch();
return 0;
OUTPUT :
Page 76
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q28. Write a program to solve Diamond problem(Hybrid inheritance and virtual base
class).
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class base //base class
{
public:
void display_base()
{
cout<<"This is base class"<<endl;
}
};
class mid_base1: virtual public base //virtual base class
{
public:
//display_base() inherited from base class
void display_mid1()
{
cout<<"This is intermediate base class1"<<endl;
}
};
class mid_base2:public virtual base //vitual base class
{
public:
//display_base() inherited from base class
void display_mid2()
{
cout<<"This is intermediate base class2"<<endl;
}
};
Page 77
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
void display_derived()
{
cout<<"This is derived class"<<endl;
}
};
int main()
{
derived d; //created object of derived class
d.display_base();
d.display_mid1();
d.display_mid2();
//accessing inherited member functions
d.display_derived();
getch();
return 0;
}
OUTPUT :
Page 78
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q29. Write a program in c++ using constructor and destructor in Multiple and multilevel
inheritance.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class base1
{
int a;
public:
base1(int a1) //constructor of base class
{
a = a1;
cout<<"This is base1 class contructor"<<endl;
}
~base1() //destructor
{
cout<<"This is base1 class destructor"<<endl;
}
void display()
{
cout<<"Value of a = "<<a<<endl;
}
};
class base2
{
int b;
public:
base2(int b1) //contrutor of base class
{
b = b1;
cout<<"This is base2 class contructor"<<endl;
}
~base2() //destructor
{
Page 79
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
}
};
int main()
{
cout<<"This is multilevel inheritance : "<<endl<<endl;
{
//multileval inheritance
derived d(2,3,4); //passing three agrument to derivered class object
d.base1::display(); //fun. overriding
d.mid_base2::display();
d.derived ::display();
}
cout<<endl<<endl;
cout<<"This is multiple inheritance : "<<endl<<endl;
{ //multiple inheritance
mid_base1 m(6,7,8); //passing three agrument to derivered class object
m.base1::display();
m.base2::display();
m.mid_base1::display();
//funcvtion overriding
}
getch();
return 0;
}
Page 81
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 82
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q30. Write a program in c++ to demonstrate pointer to an object and this pointer.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int rollno;
char name[20];
//Data members
public:
student() { } //Default constructor
ptr = &s;
//Pointer to an object
getch();
return 0;
}
OUTPUT :
Page 84
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class base
{
public:
void disp()
{
cout<<"This is base class"<<endl;
}
};
class derived:public base //single imheritance
{
public:
void disp()
{
cout<<"This is derived class"<<endl;
}
};
int main()
{
base *bptr,b; //bptr is a pointer of type base class
derived d,*dptr; //dptr is a pointer of type derived class
bptr = &d; //points address of derived class object
cout<<"Base pointer holds address of deriverd class object "<<endl;
bptr->disp();
//disp() of base class cause of bptr = &d is just ignoured by compiler at compile time
cout<<endl;
dptr = &d; //points address of own class object
cout<<"Derived pointer holds address of deriverd class object "<<endl;
dptr->disp(); //disp() of derived class
getch();
return 0;
Page 85
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 86
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q32. Create a program having pointer to void to store address of integer variable then
print value of integer variable using pointer to void. Perform the same operation for float
variable.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
void *ptr; //void pointer
int a=76;
ptr = &a; //ptr holds address of a
cout<<"Value of a is : "<<a<<endl;
cout<<"Value stored in which ptr points to is : "<<*(int*)ptr<<endl;
//type casting of void pointer to int
float b=90.99;
ptr = &b;
cout<<"Value of b is : "<<b<<endl;
cout<<"Value stored in which ptr points to is : "<<*(float*)ptr<<endl;
//type casting of void pointer to float
getch();
return 0;
}
OUTPUT :
Page 87
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q33. Create a class account that stores customer name, account number and type of
account. From this derive the classes cur_acct and sav_acct to make them more specific to
their requirements. Include necessary member functions in order to achieve the following
tasks:
a) Accept deposit from customer.
b) Display the balance
PROGRAM :
//Program to accept deposit and display the balance of saving or current account
#include<iostream>
#include<conio.h>
using namespace std;
class Account //base class
{
protected:
char cust_name[30],acct_type[30];
int account_number;
float amount;
public:
Account() //default constructor
{
amount=2000;
}
void get_detail() //member function definition
{
cout<<"Enter customer name : ";
cin>>cust_name;
cout<<"Enter account number : ";
cin>>account_number;
}
};
public:
void current();
void get_deposit();
void show_deposit(); //member fun. declaration
};
Page 88
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
public:
void saving();
void get_deposit();
void show_deposit(); //member fun. declaration
};
void sav_acct::saving() //member fun. defintion
{
get_detail();
cout<<"\nWeicome "<<cust_name<<"...."<<endl;
cout<<"It is your saving account"<<endl;
cout<<"account no. : "<<account_number<<endl;
cout<<"My amount : "<<amount<<endl;
}
void sav_acct:: get_deposit()
{
cout<<"Enter deposit amount : ";
cin>>deposit;
amount=amount+deposit;
}
void sav_acct:: show_deposit()
{
cout<<"Deposit amount is : "<<deposit<<endl;
Page 89
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
int main()
{
int i;
cur_acct obj1;
sav_acct obj2; //objects of derived class
do{
cout<<endl<<endl;
cout<<"Enter 1 for saving account."<<endl;
cout<<"Enter 2 for current account."<<endl;
cout<<"Enter 3 to exit."<<endl;
cout<<"Enter option : ";
cin>>i;
switch(i)
{
case 1:
obj1.current();
obj1.get_deposit();
obj1.show_deposit();
break;
case 2:
obj2.saving();
obj2.get_deposit();
obj2.show_deposit();
break;
case 3:
exit(0);
default:
cout<<"Enter valid option..."<<endl;
}
}while(1);
getch();
return 0;
}
Page 90
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 91
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q34. Create a class circle with data member radius; provide member function to calculate
area. Derive a class sphere from class circle; provide member function to calculate
volume.Derive class cylinder from class sphere with additional data member for height and
member function to calculate volume.
PROGRAM :
//Program to calculate volume of cylinder and sphere and area of circle through multilevel
inheritance
#include<iostream>
#include<conio.h>
using namespace std;
const float pi=22/7.0; //constant variable pi
class circle //base class
{
protected:
float radius;
//Data member in protected mode
public:
circle(float radius) //parameterized constructor
{
this->radius = radius;
}
void circle_area() //member function definition
{
cout<<"Area of circle with radius "<<radius<<" is : "<<(pi) * radius * radius<<endl;
}
};
class sphere:public circle //intermediate base class
{
public:
sphere(float r):circle(r) //constructor calling statement for base class
{
}
void sphere_volume() //member function defintion
{
float volume = (4.0/3)*(pi)*radius*radius*radius;
cout<<"Volume of Sphere with radius "<<radius<<" is : "<<volume<<endl;
}
};
Page 92
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
return 0;
}
OUTPUT :
Page 94
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class vector
{
float x;
float y;
float z; //data members
public:
vector(){ } //default constructor
vector(float x,float y,float z) //parameterized constructor
{
this->x = x;
this->y = y;
this->z = z;
}
void display(); //member functions declaration
void operator-(); //- operator overloading declaration
void operator++(); //++ operator overloading declaration
void operator--(); //-- operator overloading declaration
};
void vector::display() //member function definition
{
cout<<"Vector : "<<x<<"i + "<<y<<"j + "<<z<<"k"<<endl;
}
void vector::operator-() //- operator overloading definition
{
x = -x;
y = -y;
z = -z;
}
void vector::operator++() //++ operator overloading definition
{
++x;
++y;
Page 95
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
++z;
}
OUTPUT :
Page 96
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
PROGRAM :
#include<iostream>
#include<conio.h>
{ float x;
float y;
public:
//operator overloading
};
Page 97
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
cin>>x>>y>>z; }
{ sample temp;
temp.x = x + s.x;
temp.y = y + s.y;
temp.z = z + s.z;
return temp;
{ sample temp;
temp.x = x * s.x;
temp.y = y * s.y;
temp.z = z * s.z;
return temp;
{ sample temp;
temp.x = x / s.x;
temp.y = y / s.y;
temp.z = z / s.z;
return temp; }
return true;
else
return false;
int main()
sample samp1,samp2,samp3;
samp1.get_sample();
samp2.get_sample();
samp3.disp_sample();
samp3.disp_sample();
samp3.disp_sample();
samp1.get_sample();
samp2.get_sample();
else
Page 99
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
samp1.get_sample();
samp2.get_sample();
else
getch();
return 0;
OUTPUT :
Page 100
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q37. Create class Polar having data members radius and angle.It contains member
functions for taking input in data member function for displaying value of data members .
Class Polar contains declaration of friend function add which accepts two objects of class
Polar and returns objects of class Polar after addition.Test the class using main function
and object of class Polar.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class Polar
{
float radius;
float angle; //data members
public:
void input()
{
cout<<"Enter radius : ";
cin>>radius;
cout<<"Enter angle : ";
cin>>angle;
}
void display() //member fun. definition
{
cout<<"Radius = "<<radius<<endl;
cout<<"Angle = "<<angle<<endl;
}
friend Polar add(Polar,Polar); //friend function declaration
};
Polar add(Polar p1,Polar p2) //friend function definition
{
Polar temp;
temp.radius = p1.radius + p2.radius;
temp.angle = p1.angle + p2.angle;
return temp;
//Adding two Polar object then return a Polar object
Page 101
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
}
int main()
{
Polar p1,p2,addition; //objects created
p1.input();
p2.input();
cout<<endl;
addition = add(p1,p2); //calling friend function add(Polar,Polar)
cout<<endl<<"First object : "<<endl;
p1.display();
cout<<endl<<"Second object : "<<endl;
p2.display();
cout<<endl<<"Addition of first & second : "<<endl;
addition.display();
getch();
return 0;
}
OUTPUT :
Page 102
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q38. Write program to create a class distance having data members feet and inch (A single
object will store distance in form such as 5 feet 3 inch).
It contains member functions for taking input in data members and member function for
displaying value of data members.
Class Distance contains declaration of friend finction add which accepts two objcts of class
distance and return objects of class Distance after addition .
Class Distance contains declaration of another friend finction Subtract that accepts two
objcts of class distance and return objects of class Distance after subtraction.
Test the class using main function and objects of class Distance.
PROGRAM :
//Program to add and subtract two Distance objects using friend function
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
class Distance
{
int feet;
int inch; //Data members
public:
void input()
{
cout<<"Enter Feet : ";
cin>>feet;
cout<<"Enter Inch : ";
cin>>inch;
}
void display() //member fun. definition
{
cout<<"Distance is = "<<abs(feet)<<" Feet "<<abs(inch)<<" Inch"<<endl;
}
friend Distance add(Distance,Distance);
friend Distance subtract(Distance,Distance);
//Friend function declaration
};
Page 103
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
if(d1.feet>d2.feet)
{
if(d1.inch<d2.inch)
{
d1.feet = d1.feet - 1;
d1.inch = d1.inch + 12;
}
temp.feet = d1.feet - d2.feet;
temp.inch = d1.inch - d2.inch;
}
if(d1.feet<d2.feet)
{
if(d1.inch>d2.inch)
{
d2.feet = d2.feet - 1;
d2.inch = d2.inch + 12;
}
temp.feet = d2.feet - d1.feet;
temp.inch = d2.inch - d1.inch;
}
return temp;
}
int main()
{
Distance dis1,dis2,addition,subtraction;
Page 104
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
dis1.input();
dis2.input();
addition = add(dis1,dis2); //friend function calling
subtraction = subtract(dis1,dis2); //friend function calling
cout<<endl<<"First Distance object : ";
dis1.display();
cout<<endl<<"Second Distance object : ";
dis2.display();
cout<<endl<<"Addition : ";
addition.display();
cout<<endl<<"Subtraction : ";
subtraction.display();
getch();
return 0;
}
OUTPUT :
Page 105
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q39. Write a program to create class Mother having data member to store salary of
Mother,create another class Father having data member to store salary of Father.
Write a friend function ,which accepts objects of class Mother , and Father and Prints Sum
of salary of Mother and Father objects.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class Father
{
float salary;
public:
void input()
{
cout<<"Enter salary of Father : ";
cin>>salary;
}
Page 106
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
OUTPUT :
Page 107
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q40. Write a program to create class having data member to store salary of Mother ,
create another class Father having data member to store salary of Father. Declare class
Father to be friend class of Mother. Write a member function in Father, which accepts
object of class Mother and prints Sum of Salary of Mother and Father Objects. Create
member function in each class to get input in data member and to display the value of data
member.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
void input()
{
cout<<"Enter salary of Father : ";
cin>>salary;
}
void display()
{
cout<<"salary of Father : "<<salary<<endl;
}
OUTPUT :
Page 109
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Q41. Create a base class shape having two data members with two-member function
getdata (pure virtual function) and printarea (not pure virtual function).
Derive classes triangle and rectangle from class shape and redefine member function
printarea in both classes triangle and rectangle and test the functioning of classes using
pointer to base class objects and normal objects.
PROGRAM :
#include<iostream>
#include<conio.h>
using namespace std;
class shape //abstract base class
{
int a;
float b;
public:
virtual void getdata() = 0; //pure virtual function definition
virtual void printarea() //virtual function definition
{
cout<<"Lets print the area you want "<<endl;
}
};
class tringle:public shape //tringle is derived from shape class
{
Page 110
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
float height;
float length;
public:
void getdata()
{
cout<<"Enter height and lenth of tringle : ";
cin>>height>>length;
}
void printarea()
{
cout<<"Area of Tringle is : "<<0.5*height*length<<endl;
}
};
class rectangle:public shape //rectangle is derived from shape class
{
float width;
float length;
public:
void getdata()
{
cout<<"Enter length and width of rectangle : ";
cin>>length>>width;
}
void printarea()
{
cout<<"Area of Rectangle is : "<<length * width<<endl;
}
};
int main()
{
shape *base_ptr[2]; //pointer of base class
tringle t;
rectangle r;
base_ptr[0] = &t; //holds address of tringle class object
base_ptr[1] = &r; //holds address of rectangle class object
cout<<endl<<"Base pointer 1 holds address of tringle class ojject"<<endl;
base_ptr[0]->getdata();
base_ptr[0]->printarea();
cout<<endl<<endl<<"Base pointer 2 holds address of rectangle class ojject"<<endl;
base_ptr[1]->getdata();
base_ptr[1]->printarea();
getch();
return 0;
}
OUTPUT :
Page 111
Author name : Suryakant MasterProgramming.in
Path : E:\Assignment\ cpp\
Page 112
Author name : Suryakant MasterProgramming.in