Practical C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Index

S/N Name of program Page Signature


no.
1. Write a program to create a class to store student 1-2
information with data members as roll no, name,
marks, in 3 subjects total and average using
constructor where ever required.
2. Write a program to implement various operator. 3-5

3. Write a program to implement 2-D array. 6

4. Write a program to implement array of object. 7

5. Write a program to implement const members and 8-9


member function.

6. Write a program to implement static data members 10-11


and static member function.

7. Write a program to implement friend functions and 12-13


friend classes.

8. Write a program to various types of constructor. 14-16

9. Write a program to implement virtual destructor. 17

10. Write a program to implement dynamic memory 18


allocation using new and delete operator.

11. Write a program to read an array and display an 19


array using dynamic memory allocation.

12. Write a program to implement various types of 20-21


inheritance.

13. Write a program to implement role of constructor 22-23


and destructor in inheritance.

14. Write a program to implement virtual function and 24-25


abstract class concept
15. Write a program to create memory space for a class 26-27
object using new operator and to destroy it using
delete operator.
16. Develop an object oriented program in c++ to read 28-29
emp name, emp code, designation, experience and
age. Construct the database with suitable member
functions for initializing and destroying the data
using constructor and destructor and dynamic
memory allocation operators new and delete.
17. Write a program in c++ to prepare mark sheet of an 30-31
university exam by reading stuname, rollno, sub
name, subcode, internal marks, external marks.
Design a base class consisting data members such
as student name, roll no, sub name. Derived class
consists data members such as sub code, internal
marks, external marks.
PROGRAM:- 1 Write a program to create a class to store student information with data
members as roll no, name, marks, in 3 subjects total and average using constructor where
ever required.
#include<iostream.h>
#include<string.h>
class student
{
private:
int rollno;
char name[50];
float marks[3];
float total;
float average;
public:
//constructor to initialize roll number and name
student(int rno, const char* studentname){
rollno = rno;
strcpy(name, studentname);
total = 0;
average = 0;
}
//function to input marks
void inputmarks(){
cout<<"enter marks for 3 subjects:";
for(int i=0;i<3;i++){
cin>>marks[i];
total+=marks[i];
}
calculateaverage();
}
//function to calculate average
void calculateaverage(){
average = total/3;
}
//function to display student information
void displayinfo(){
cout<<"\n student information:\n";
cout<<"roll no:"<<rollno<<endl;
cout<<"name:"<<name<<endl;
cout<<"marks:"<<marks[0]<<","<<marks[1]<<","<<marks[2]<<endl;
cout<<"total marks:"<<total<<endl;
cout<<"average marks:"<<average<<endl;
}
};
int main()
{
int rollno;
char name[50];

[1]
cout<<"enter roll number:";
cin>>rollno;
cout<<"enter name:";
cin.ignore();
cin.getline(name, 50);
student student(rollno, name);
student.inputmarks();
student.displayinfo();
return 0;
}

OUTPUT:-

[2]
PROGRAM 2:- Write a program to implement various operator.
1.Arithmetic:-
#include<iostream.h>
#include<conio.h>
class default_constuct
{
public:
int a, b;
default_constuct()
{
a = 100;
b = 200;
}
};
int main()
{
default_constuct con;
cout<<"value of a:"<<con.a;
cout<<"value of b:"<<con.b;
return 0;
}

Output:

[3]
2.Relation:-

#include<iostream.h>
#include<conio.h>
int main()
{
int a, b;
a=3;
b=5;
int result;
result=(a==b); //false
cout<<"3==5 is"<< result;
result=(a!= b); //true
cout<<"3!=5 is"<<result;
result=a>b; //false
cout<<"3>5 is" <<result;
result=a<b; //true
cout<<"3<5 is" <<result;
result=a>=b; //false
cout<<"3>=b is"<<result;
result=a<=b; //true
cout<<"3<=5 is"<<result;
return 0;
}

OUTPUT:-

[4]
3.Logical:-
#include<iostream.h>
#include<conio.h>
int main()
{
int result;
result=(3!=5) && (3<5);
cout<<"(3!=5) && (3<5) is"<<result;
result=(3==5) && (3<5);
cout<<"(3==5) && (3<5) is"<<result;
result=(3==5) && (3>5);
cout<<"(3==5) && (3>5) is"<<result;
result=(3!=5) || (3<5);
cout<<"(3!=5) || (3<5) is"<<result;
result(3!=5) || (3>5);
cout<<"(3!=5) || (3>5) is "<<result;
result=!(5==2);
cout<<"!(5==2) is"<<result;
result=!(5==5);
cout<<"!(5==5) is"<<result;
return 0;
}

OUTPUT:-

[5]
PROGRAM:-3 Write a program to implement 2-D array.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,r,c;
clrscr();
cout<<"\n enter number of rows and column of a matrix";
cin>>r>>c;
cout<<"\n enter elements of"<<r<< "rows and" <<c<< "column\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
for(i=0;i<u;i++)
{
for(j=0;j<c;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
getch();
}

OUTPUT:-

[6]
PROGRAM:-4 Write a program to implement array of object.
#include<iostream.h>
const int MAX = 100;
class details
{
private:
float salary;
int roll;
public:
void getdata()
{
cout<<"\n enter the salary:";
cin>>salary;
cout<<"\n enter the roll:";
cin>>roll;
}
void putdata()
{
cout<<"employee salary is"<<salary<<"and roll is"<<roll<<"\n";
}
};
void main()
{
details d[MAX];
int n=0;
char ans;
do
{
cout<<"enter the employee number:"<< n+1;
d[n++].getdata();
cout<<"enter another (y/n)?:";
cin>>ans;
}while(ans!='n');
for(int j=0;j<n;j++)
{
cout<<"\n employee number is:"<<j+1;
d[j].putdata();
}

OUTPUT:-

[7]
PROGRAM: -5 Write a program to implement const members and member function.
#include<iostream.h>
#include<stdio.h>
#include<iomanip.h>
class book
{
private:
int book_no;
char book_title[20];
float price;
float total_cost(int n);
public:
void input();
void purchase();
};
void book :: input()
{
cout<<"\n enter the book no";
cin>>book_no;
cout<<"\n enter book title";
gets(book_title;
fflush(stdin);
cout<<"\n enter price";
cin>>price;
}
float book :: total_cost (int n)
{
return price * n;
}
void book :: purchase()
int num;
float cost;
cout<<"\n enter the no. of copies to be purchased";
cin>>num;
cost = total_cost (num);
cout<<"\n the cost =";
cout<<cost;
}
void main()
{
book bob;
int choice;
do
{
cout<<"\n MENU";
cout<<"\n INPUT 1";
cout<<"\n purchase 2";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1: bob.input();
break;
case 2: bob.purchase();
}
}
while(choice!=3);}

[8]
OUTPUT:-

[9]
PROGRAM: -6 Write a program to implement static data members and static member
function.
#include<iostream.h>
#include<conio.h>
class first
{
private:
static int x;
public:
first ();
static void display ()
{
cout<<"the value of x after incremented by one ="<<x;
}
};
class second
{
private:
int y;
public:
void getdata ()
{
cout<<"enter the value";
cin>>y;
}
void display ()
{
cout<<"contents of y="<< this -> y;
}
};
int first: x = 25;
first: first ()
{
++x;
}
void main ()
{
clrscr();
second s;
s.getdata();
first :: display();
s.diaplay();
getch();
}

[10]
OUTPUT:-

[11]
PROGRAM:-7 Write a program to implement friend function and friend classes.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//class declaration
class matrix
{
int i, j;
int m, n;
int mat[10][10];
public:
void read_mat();
//declaration of friend function
friend matrix add_mat(matrix x, matrix y);
void display_mat();
};
//defines function read_mat()
void matrix :: read_mat()
{
cout<<"\n enter the size of the matrix: m, n";
cin>>m>>n;
cout<<"\n enter the matrix:";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat[i][j];
}
void matrix :: display_mat()
{
cout<<"\n the matrix is:";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<mat[i][j]<<"";
}
}
//defines the function add_mat()
matrix add_mat(matrix x, matrix y)
{
matrix z;
int k, l;
if((x.m == y.m) && (x.n == y.n))
z.m = x.m;
for(k=0;k<x.m;k++)
{
for(l=0;l<x.n;l++)
z.mzt[k][l]=x.mat[k][l] + y.mat[k][l];
}
{
cout<<"\n the matrice can not be added";

[12]
exit (0);
}
return(z);
}
void main ()
{
matrix a, b, c;
clrscr();
a.read_mat(); //read matrix a
b.read_mat(); //read matrix b
c = add_mat(a,b);
c.display_mat();
getch();
}

OUTPUT:-

[13]
PROGRAM:-8 Write a program to implement various types of constructor.
1-Default constructor:
#include<iostream.h>
#include<conio.h>
Class default_construct
{
Public:
Int a, b;
Default_consrtuct()
{
A = 100;
B = 200;
}
};
Int main()
{
Default_construct con;
Cout<<”value of a:”<<con.a;
Cout<<”value of b:”<<con.b;
Return 0;
}

OUTPUT:-

[14]
2-Parameterized constructor:
#include<iostream.h>
#include<conio.h>
class rectengle
[
private:
double length;
double breadth;
public:
rectengle(double l, double b)
{
length = l;
breadth = b;
}
duble calculate area ()
{
return length * breadth;
}
};
int main ()
rectengle obj1(10,6);
rectengle obj2(13,8);
cout<<"area of rectengle 1:"<<obj1.calculatearea();
cout<<"area of rectengle 2:"<<obj2.calculatearea();
return 0;
}

OUTPUT:-

[15]
3- Copy constructor:
#include<iostream.h>
#include<conio.h>
class rectengle
{
private:
double lentgh;
double breadth;
public:
rectengle (double l, double b)
{
length = l;
breadth = b;
}
rectengle (rectengle &obj)
{
length = ob. Length;
breadth=ob. Breadth;
}
double calculate area ()
{
return length*breadth;
}
};
int main ()
{
rectengle obj1(10,6);
rectengle obj2 = obj1;
cout<<"area of rectengle:"<<obj1.calculatearea();
cout<<"area of rectengle 2:"<<obj2.calculatearea();
return 0;
}

OUTPUT:-

[16]
PROGRAM: -9 Write a program to implement virtual destructor.
#include<iostream.h>
#include<conio.h>
class area
{
int length, breadth;
public:
area (int l, int b)
{
length=l;
breadth=b;
}
void display()
{
cout<<"area of rectengle is:"<<length*breadth;
}
};
void main()
{
clrscr();
area a1(8,7);
a1.display()
getch();
}

OUTPUT:-

[17]
PROGRAM: -10 Write a program to implement dynamic memory allocation using new and
delete operation.
#include<iostream.h>
#include<conio.h>
class dcons
{
int *p;
public:
dcons ()
{
p=new int;
*p=100;
}
dcons (int v)
{
p=new int;
*p=v;
}
int dis ()
{
return (*p);
}
};
void main()
{
dcons o,o1(9);
cout<<"the value of object o's p is:";
cout<<o.dis();
cout<<"\n the value of object o1's p is:"<<o1.dis();
getch();
}

OUTPUT:-

[18]
PROGRAM:-11 Write a program to read an array and display an array using dynamic
memory allocation
#include<iostream.h>
void main()
{
int*p,n,i;
p=new int [10];
cout<<"how many elements you want to enter:";
cin>>n;
cout<<"\n enter the elements";
for(i=0;i<n;i++)
cin>>p[i];
cout<<"the entered elements are"<<end1;
for(i=0;i<n;i++)
cout<<p[i]<<"";
delete[] p;
}

OUTPUT:-

[19]
PROGRAM: - 12 Write a program to implement various types of inheritance.
#include<iostream.h>
#include<conio.h>
class addition
{
private:
int sum_a;
protected:
int sum_b;
};
class display : public addition
{
private:
int x;
protected:
int y;
public:
void getdata();
void sumdata();
};
void display :: getdata ()
{
cout<<"enter the value of x:";
cin>>x;
cout<<"enter the value of y:";
cin>>y;
}
void display :: sumdata()
{
sum_b=x+y;
cout<<"\n the addition of x and y is:"<<sum_b;
}
void main()
{
class display obhj;
clrscr();
obj.getdata();
obj.sumdata();
getch(){

[20]
OUTPUT:-

[21]
PROGRAM: - 13 Write a program to implement role of constructor and destructor in
inheritance.

#include<iostream.h>
class A
{
public:
float height, weight;
};
class B : public A
{
public:
int marks_sub1, marks_sub2, marks_sub3, tot_marks;
};
class C : public B
{
private:
char name[20], course[10];
int roll;
public:
void getdetails();
void display();
};
void C :: getdetails()
{
cout<<"enter the roll number of student:";
cin>>roll;
cout<<"enter the course of student:";
cin>>course;
cout<<"enter the name of student:";
cin>>name;
cout<<"enter the height of student (int inches):";
cin>>height;
cout<<"enter the weight of student (int kg):";
cin>>weight;
cout<<"enter the marks in first subject:";
cin>>marks_sub1;
cout<<"enter the marks in second subject:";
cin>>marks_sub2;
cout<<"enter the marks in third subject:";
cin>>marks_sub3;
}
void C :: display()
{
tot_marks= marks_sub1+marks_sub2+marks_sub3;
cout<<"the student's details are"<<endl;
cout<<"the roll number of student is:"<<roll<<endl;
cout<<"the course name of student is:"<<course<<endl;
cout<<"the name of student is:"<<name<<endl;
cout<<"the height of student is (in inches):"<<height<<endl;

[22]
cout<<"the weight of student is (in kg):"<<weight<<endl;
cout<<"total marks of student:"<<tot_marks;
}
void main()
{
C obj;
obj.getdetails();
obj.display();
}

OUTPUT:-

[23]
PROGRAM:- 14 Write a program to implement virtual function and abstract class concept.
#include<iostream.h>
#include<conio.h>
class personal
{
public:
char name[20], course[10];
int roll;
};
class academic : virtual public personal
{
protected:
int exam_percent_marks;
};
class class_performance : virtual public personal
{
protected:
int class_percent_marks;
};
class student : public academic, public class_performance
{
private:
int tot_percent_marks;
public:
void getdetails();
void display();
};
void student :: getdetails()
{
cout<<"enter the roll number of student:";
cin>>roll;
cout<<"enter the course of student:";
cin>>course;
cout<<"enter the name of student:";
cin>>name;
cout<<"enter the percentage of marks examination:";
cin>>exam_percent_marks;
cout<<"enter the percentage of marks in class test:";
cin>>class_percent_marks;
}
void student :: display()
{
tot_percent_marks=((exam_percent_marks +
class_percent_marks)/2);
cout<<"the student's details are";
cout<<"the roll number of student is:"<<roll;
cout<<"enter course name of student is:"<<course;
cout<<"the name of student is:"<<name;
cout<<"percentage of marks in
examinatin:"<<exam_percent_marks;

[24]
cout<<"percentage of marks in calss
test:"<<class_percent_marks;
cout<<"total percentage of marks of
student:"<<tot_percent_marks;
}
void main()
{
student obj;
obj.getdetails();
clrscr();
obj.display();
getch();
}

OUTPUT:-

[25]
PROGRAM:- 15 Create memory space for a class object using new operation and to
destroy it using delete operation.
#include<iostream.h>
#include<conio.h>
class employee
{
private:
char ssid[10, name[20], address[30], dept[20];
int age;
long salary;
public:
void read();
void show();
};
void employee :: read()
{
cout<<"\n enter the following details an employee"<<endl;
cout<<"\n SSID:";
cin>>ssid;
cout<<"\n name:";
cin>>name;
cout<<"\n age:";
cin>>age;
cout<<"\n dept. name:";
cin>>dept;
cout<<"\n salary:";
cin>>salary;
}
void employee :: show ()
{
cout<<"the details of an employee are"<<endl;
cout<<"\n SSID is:"<<ssid<<endl;
cout<<"\n name is:"<<name<<endl;
cout<<"\n age is:"<<age<<end;
cout<<"\n dept. name is:"<<dept<<endl;
cout<<"\n salary:"<<salary<<endl;
}
void main()
{
clrscr();
employee *p;
p=new employee; //allocated memory space for class object
using new operator
p->read();
clrscr();
p->show();
delete p; //de-allocated memory space of class object using
delete operator

[26]
getch();
}
OUTPUT:-

[27]
PROGRAM:- 16 Develop an object oriented program in c++ to read emp name, emp code,
designation, experience and age. Construct the database with suitable member functions
for initializing and destroying the data using constructor and destructor and dynamic
memory allocation operators new and delete.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class employee
{
private:
char emp_code[5], emp_name[20],desig[20];
int age, experience;
public:
void getdetails();
void display();
employee() //constuctor
{
age, experience=0;
strcpy(emp_name, "");
}
~employee() //destuctor
{
cout<<"data members are deleted";
}
};
void employee :: getdetails()
{
cout<<"enter the employee code:";
cin>>emp_code;
cout<<"enter the employee name:";
cin>>emp_name;
cout<<"enter the employee's age:";
cin>>age;
cout<<"enter the employee designation:";
cin>>desig;
cout<<"enter the employee's experience:";
cin>>experience;
}
void employee :: display()
{
cout<<"the details of employee are"<<endl;
cout<<"employee code:"<<emp_code<<endl;
cout<<"employee name:"<<emp_name<<endl;
cout<<"employee's age:"<<age<<endl;
cout<<"employee's designation:"<<desig<<endl;
cout<<"employee's experience:"<<expeience<<endl;

[28]
}
void main()
{
employee *p;
p=new employee[10]; //allocation of memory using new operator
int i, n;
cout<<"how many records you want to enter";
cin>>n;
for(i=0;
i<n;i++)
p[i].getdetails();
cout<<"the entered records are"<<endl;
for(i=0;i<n;i++)
p[i].display();
delete []p; //de-allocation of memory using delete oprerator
getch();
}

OUTPUT:-

[29]
PROGRAM:- 17 Write a program in c++ to prepare mark sheet of an university exam by
reading stuname, rollno, sub name, subcode, internal marks, external marks. Design a base
class consisting data members such as student name, roll no, sub name. Derived class
consists data members such as sub code, internal marks, external marks.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class basic_info
{
protected:
char stu_name[20], father_name[20], sub_name[15];
int roll_no;
public:
void get_basic-info()
{
cout<<"enter the name of student:"<<endl;
cin>>stu_name;
cout<<"enter the name of student's father:"<<endl;
cin>>father_name;
cout<<"enter the roll number of student:"<<endl;
cin>>roll_no;
cout<<"enter the subject name:"<<endl;
cin>>sub_name;
}
};
class student_info : public basic_info
{
private:
char sub_code[6],result[5];
int marks_int, marks_ext, marks_tot;
public:
void getdetails();
void display();
};
void student_info :: getdetails()
{
basic_info :: get_basic_info();
cout<<"enter subject code (e.g.BCA121):"<<endl;
cin>>sub_code;
cout<<"enter internal marks (mm:50):"<<endl;
cin>>marks_int;
cout<<"enter external marks (mm:50):"<<endl;
cin>>marks_ext;
}
void student_info :: display()
{
marks_tot=marks_int + marks_ext;
if(marks_tot>=40)
strcpy(result,"PASS");
else
strcpy(result,"FAIL");
clrscr();
cout<<"punjabi university patiala"<<endl;
cout<<"statement of marks"<<endl;

[30]
cout<<endl;
cout<<endl;
cout<<"roll number:"<<roll_no<<endl;
cout<<"student's name:"<<stu_name<<endl;
cout<<"father's name:"<<father_name<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"|";
cout<<"subject code"<<'\t'<<"subject name"<<'\t'<<"internal
marks"<<'\t'<<"external marks"<<"|"<<endl;
cout<<"---------------------------------------------------"<<endl;
cout<<"|";
cout<<sub_code<<'\t'<<'\t'<<sub_name<<'\t'<<'\t'<<marks_int<<'\t'<<'\t'
<<marks_ext<<"|"<<endl;
cout<<endl;
cout<<endl;
cout<<"result:"<<result<<'\t'<<'\t'<<'\t'<<'\t'<<"total marks
obtained:"<<marks_tot;
}
void main()
{
student_info student[10];
int n,i,loc;
cout<<"how many records you want to enter:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter record number:"<<i+1<<endl;
student[i].getdetails();
}
cout<<"now enter the record you want to print";
cin>>loc;
student[loc - 1].display();
getch();
}

OUTPUT:-

[31]
[32]

You might also like