Practical No 10 To 14

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Practical No:10

➢Program to Implement Constructors and Destructors


XIII. EXERCISE:
10.1 Write a C++ program to define a class "Number" having data members x and y and perform mathematical operations like
addition, subtraction, multiplication and division on two numbers using constructor and destructor functions.

Code:
#include<iostream.h>
#include<conio.h>
class number
{
float x,y;
Number()
{
x=5;
y=8;
}
public:
void get()
{
cout<<"Enter two numbers to perform Mathematical opetration"<<endl;
cout<<"(Addition,Substraction, Multiplication, Division)"<<endl;
cin>>x>>y;
}
void add()
{
cout<<"The Addition of two numbers is= "<<x+y<<endl;
}
void sub()
{
cout<<"The Substraction of two numbers is= "<<x-y<<endl;
}
void mul()
{
cout<<"The Multiplication of two numbers is= "<<x*y<<endl;
}
void div()
{
cout<<"The Division of two numbers is= "<<x/y<<endl;
}
};
void main()
{
clrscr();
cout<<endl<<"Output Prepared by Roll No=425 Omkar Shete."<<endl<<endl;
number n;
n.get();
n.add();
n.sub();
n.mul();
n.div();
getch();
}
OUTPUT:
Practical No:-11
➢Program to Implement Single Inheritance.
XIII. EXERCISE:
11.1 Write a C++ program to define a class "Student" having data members roll_no, name. Derive
a class "Marks" from "Student" having data members m1.m2,m3, total and percentage. Accept
and display data for one student.

Code:
#include<iostream.h>
#include<conio.h>
class student
{
public:
int rollno;
char name[20];
void get1()
{
cout<<endl<<endl<<"Enter your Name:-";
cin>>name;
cout<<endl<<"Enter your Rollno:-";
cin>>rollno;
}
};
class marks:public student
{
public:
int m1,m2,m3,total;
float percentage;
void get2()
{
cout<<endl<<"Enter your First Subject Marks:-";
cin>>m1;
cout<<endl<<"Enter your Second Subject Marks:-";
cin>>m2;
cout<<endl<<"Enter your Third Subject Marks:-";
cin>>m3;
}
void cal()
{
total=m1+m2+m3;
percentage=total/3;
}
void display()
{
cout<<endl<<endl<<"Your Result Details are as follow";
cout<<endl<<"Name="<<name;
cout<<endl<<"Roll No="<<rollno;
cout<<endl<<"Marks of First Subject="<<m1;
cout<<endl<<"Marks of Second Subject="<<m2;
cout<<endl<<"Marks of Third Subject="<<m3;
cout<<endl<<"Total Marks Obtain="<<total;
cout<<endl<<"Percentage="<<percentage;
}
};
void main()
{
clrscr();
marks m;
cout<<endl<<"Output Prepared by Roll No=425 Omkar Shete";
m.get1();
m.get2();
m.cal();
m.display();
getch();
}

OUTPUT:-
Practical No:-11
➢Program to Implement Single Inheritance.
XIII. EXERCISE:
11.2 Write a C++ program to define a class "Employee" having data members emp_no, emp_name and
emp_designation. Derive a class "Salary" from "Employee" having data members basic, hra, da,
gross_sal. Accept and display data for one employee.

Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Employee
{
public:
int emp_no;
char emp_name[40],emp_designation[40];
void get1()
{
cout<<endl<<endl<<"Enter Employee Name:- ";
gets(emp_name);
cout<<"Enter Employee Number:- ";
cin>>emp_no;
cout<<"Enter Employee Designation:-";
cin>>emp_designation;
}
};
class Salary:public Employee
{
public:
double basic,hra,da,gross_sal;
void get2()
{
cout<<endl<<"Enter your salary:- ";
cin>>basic;
hra=0.3*basic;
da=0.745*basic;
gross_sal=basic+hra+da;
}
void set()
{
cout<<endl<<"Employee Name is :- "<<emp_name;
cout<<endl<<"Employee Number is :- "<<emp_no;
cout<<endl<<"Employee Designation is :- "<<emp_designation;
cout<<endl<<"Employee Salary is :- "<<basic;
cout<<endl<<"Employee Salary hra is :- "<<hra;
cout<<endl<<"Employee Salary da is :- "<<da;
cout<<endl<<"Employee Gross salary is :- "<<gross_sal;
}
};
void main()
{
clrscr();
cout<<"Output Prepared by Roll No:- 425 Omkar Shete.";
Salary s;
s.get1();
s.get2();
s.set();
getch();
}

OUTPUT:-

Practical No:- 13
➢ Program to Implement Concept Multiple Inheritance.
XIII. EXERCISE:
13.1 Write a C++ program to calculate the area and perimeter of rectangles
using concept of inheritance.

CODE:-
#include<iostream.h>
#include<conio.h>
class Area
{
public:
int len,br,a;
void area(int l,int b)
{
len=l;
br=b;
a=len*br;
}
};
class Perimeter
{
public:
int len,br,p;
void perimeter(int l,int b)
{
len=l;
br=b;
p=2*(len+br);
}
};
class rectangle:public Area, public Perimeter
{
public:
int length,breath;
public:
void getdata()
{
cout<<endl<<"Enter values for Length :- ";
cin>>length;
cout<<endl<<"Enter values for Breath :- ";
cin>>breath;
area(length,breath);
perimeter(length,breath);
}
public:
void display()
{
cout<<endl<<"Area is :- "<<a;
cout<<endl<<"Perimeter is :- "<<p;
}
};
void main()
{
clrscr();
cout<<endl<<"Output Prepared by Roll No:- 425 Omkar Shete"<<endl;
rectangle r;
r.getdata();
r.display();
getch();
}

OUTPUT:-

Practical No:- 13
➢ Program to Implement Concept Multiple Inheritance.
XIII. EXERCISE:
13.2 Write a C++ program for representation of class hierarchy as below Assume
suitable data and function members.

INPUT:-

#include<iostream.h>
#include<conio.h>
class cricketer
{
char c_name[20];
long c_score;
public:
void enter1()
{
cout<<endl<<"Enter Cricketer's Name and Score:";
cin>>c_name>>c_score;
}
void display1()
{
cout<<endl<<"Cricketer's Name : "<<c_name;
cout<<endl<<"Cricketer's Score: "<<c_score;
}
};
class bowler:public virtual cricketer
{
char b_name[20];
long wickets;
public:
void enter2()
{
cout<<endl<<"Enter Bowler's Name and Number of Wickects taken: ";
cin>>b_name>>wickets;
}
void display2()
{
cout<<endl<<"Bowler's Name: "<<b_name;
cout<<endl<<"Numbers of Wickets taken: "<<wickets;
}
};
class batsman:public virtual cricketer
{
long runs;
char bt_name[20];
public:
void enter3()
{
cout<<endl<<"Enter Batsman Name and number of runs made:";
cin>>bt_name>>runs;
}
void display3()
{
cout<<endl<<"Batsman's Name: "<<bt_name;
cout<<endl<<"Number of Runs made:"<<runs;
}
};
class allrounder:public bowler,public batsman
{
long a_score;
char a_name[20];
public:
void enter4()
{
cout<<endl<<"Enter Allrounder's Name and Score: ";
cin>>a_name>>a_score;
}
void display4()
{
cout<<endl<<"Allrounders Name: "<<a_name;
cout<<endl<<"Allrounders Score: "<<a_score;
}
};
void main()
{
allrounder n;
clrscr();
cout<<endl<<"Output Prepared by Roll no:- Omkar Shete"<<endl;
n.enter1();
n.enter2();
n.enter3();
n.enter4();
n.display1();
n.display2();
n.display3();
n.display4();
getch();
}

OUTPUT:-
Practical No:- 14
➢ Program to Implement Pointer of object
XIII.EXERCISE:-
14.1 Write a C++ program to declare a class "Book" containing data members book_name,
auther_name, and price .Accept this information for one object of the class using pointer to that
object.
INPUT:-
#include<iostream.h>
#include<conio.h>
class Book
{
public:
char book_name[20];
char author_name[20];
float price;
public:
void get()
{
cout<<"Enter Book Name="<<endl;
cin>>book_name;
cout<<"Enter Author Name="<<endl;
cin>>author_name;
cout<<"Enter Book Price="<<endl;
cin>>price;
}
void display()
{
cout<<"Book Name="<<book_name<<endl;
cout<<"Book Author Name="<<author_name<<endl;
cout<<"Book Price="<<price<<endl;
}
};
void main()
{
clrscr();
cout<<"Output Prepared by Roll No=425 Omkar Shete"<<endl;
Book *b,b1;
b->get();
b->display();
getch();
}
OUTPUT:-

Practical No:-
14
➢ Program to Implement Pointer of object
XIII.EXERCISE:-
14.2 Write a C++ program to declare a class "Box" having data members height, width and breadth. Accept this
information for one object using pointer to that object Display the area and volume of that object.

INPUT:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class box
{
int height,width, breadth,area,vol;
public:
void get()
{
cout<<endl<<endl<<"Enter value of height, weidth, breadth : ";
cin>>height>>width>>breadth;
}
void cal()
{
area=2*((height*width)+(width*breadth)+(breadth*height));
vol=height*width*breadth;
}
void show()
{
cout<<endl<<"area of box is: "<<area;
cout<<endl<<"volume of box is: "<<vol;
}
};
void main()
{
clrscr();
cout<<"Output Prepared by Roll No:- 425 Omkar Shete .";
box *b,bl;
b=&bl;
b->get();
b->cal();
b->show();
getch();
}
OUTPUT:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy