Program 16

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

Program No.

04

Write a program in C++ to demonstrate inline function.

#include<iostream.h>
#include<conio.h>
inline int cube (int a)
{
int p;
p=a*a*a;
}
main()
{
clrscr();
int n;
cout<<" enter a number"<<endl;
cin>> n;
cout<<"cube of number"<<cube(n);
getch();
return(0);
}
OUTPUT:-

enter a number
8
cube of number512
Program No.03

Write a program in C++ to implement parameterized constructor.

#include<iostream.h>
#include<conio.h>
class product
{
int productid;
double price;
public:
product(int a,double b)
{
productid=a;
price=b;
}
void display()
{
cout<<"Product id:"<<productid<<endl;
cout<<"price:"<<price<<endl;
}
};
int main()
{
clrscr();
product p1(122,50.23);
p1.display();
getch();
return 0;
}
OUTPUT:-

Product id:122
price:50.23
Program No.01

Write a program C++ to demonstrate class and object.

#include<iostream.h>
#include<conio.h>
class room
{
public:
double length,breath;
double area()
{
return length*breath;
}
};
int main()
{
clrscr();
room r1;
r1.length;
r1.breath;
cout<<"enter a length value:";
cin>>r1.length;
cout<<"enter a breath value:";
cin>>r1.breath;
cout<<"the area of room is :"<<r1.area();
getch();
return 0;
}

OUTPUT:-
enter a length value:4
enter a breath value:3
the area of room is :12
Program No.05

Write a program in C++ for default argument.

-
#include<iostream.h>
#include<conio.h>
class sample
{
int x,y;
public:
void getdata(int v1,int v2=10)
{
x=v1;
y=v2;
}
void display()
{
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
}
};
int main()
{
clrscr();
sample s1;
s1.getdata(44);
s1.display();
getch();
return 0;
}

OUTPUT:-
x:44
y:10
Program No.02

Write a program in C++ to demonstrate constructor and


distructor.

#include<iostream.h>
#include<conio.h>
class test
{
public:
test()
{
cout<<"object is created"<<endl;
}
~test()
{
cout<<"object is destroyed"<<endl;
}
};
int main()
{
clrscr();
test*obj=new test();
delete obj;
getch();
return 0;
}
OUTPUT:-

object is created
object is destroyed
Program No.06

Write a program in c++ for copy constructor

#include<iostream.h>
#include<conio.h>
class rectangle
{
private:
double length;
double width;
public:
rectangle(double len,double wid)
{
length=len;
width=wid;
}
rectangle(rectangle &obj)
{
length=obj.length;
width=obj.width;
}
double cal()
{
return 2*(width+length);
}
};
int main()
{
clrscr();
rectangle r1(88.34,55.33);
rectangle r2=r1;
cout<<"parameter of rectangle r1:"<<r1.cal()<<endl;
cout<<"parameter of rectangle r2:"<<r2.cal()<<endl;
getch();
return 0;
}

OUTPUT:-
parameter of rectangle r1:287.34
parameter of rectangle r2:287.34
Program no.07

Write a program in C++ for constructor overloading.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class Person
{
char name[50];
int age;
public:
Person()
{
(name,"Shravan");
age=19;
}
Person(const char*n,int a)
{
(name, n);
age = a;
}
void showData()
{
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
}
};
int main()
{
clrscr();
Person p1;
Person p2("Soham",55);
cout<<"Data for p1"<<endl;
p1.showData();
cout<<endl;
cout<<"Data for p2:"<<endl;
p2.showD
ata();
getch();
return 0;
}

OUTPUT:-
Data for p1
Name:
Age:19

Data for p2:


Name:
Age:55
Program no.08

Write a program in C++ for implement array of object

#include<iostream.h>
#include<conio.h>
class employee
{
private:
int empid;
char name[23];
public:
void getdata();
void putdata();
};
void employee::getdata()
{
cout<<"Enter employee id is :"<<endl;
cin>>empid;
cout<<"Enter employee name is :"<<endl;
cin>>name;
}
void employee::putdata()
{
cout<<"employee id is:"<<empid<<endl;
cout<<"employee name is:"<<name<<endl;
}
int main()
{
clrscr();
employee emp[4];
int i,n;
for(i=0;i<2;i++)
{
emp[i].getdata();
}
for(i=0;i<2;i++)
{
emp[i].putdata();
}
getch();
return 0;
}
OUTPUT:-
Enter employee id is :
04
Enter employee name is :
sergio
Enter employee id is :
4
Enter employee name is :
shravan
employee id is:4
employee name is:sergio
employee id is:4
employee name is:shravan
Program no.09

Write a program in C++ to demonstrate the use of friend function

#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int meter;
friend int add_five(Distance);
public:
Distance()
{
meter=0;
}
};
int add_five(Distance d)
{
d.meter+4;
return d.meter;
}
int main()
{
clrscr();
Distance d;
cout<<"Distance is:"<<add_five(d);
getch();
return 0;
}
OUTPUT:-

Distance is:0
Program no.10

Write a program in C++ to implement “this” pointer

#include<iostream.h>
#include<conio.h>
class Department
{
int depno;
public:
Department (int depno)
{
this->depno=depno;
}
void display()
{
cout<<"Department number is:"<<depno<<endl;
}
};
int main()
{
clrscr();
Department d1(44);
d1.display();
getch();
return 0;
}
OUTPUT:-

Department number is:44


Program no.11

Write a program to impliment single inheritance.

#include<iostream.h>
#include<conio.h>
class Parent
{
public:
int p_age;
Parent()
{
p_age=33;
}
};
class Child:public Parent
{
public:
int c_age;
Child()
{
c_age=19;
}
void Dhisplay()
{
cout<<"The parent age is:"<<p_age<<endl;
cout<<"The child age is:"<<c_age<<endl;
}
};
int main()
{
clrscr();
Child c1;
c1.Display();
getch();
return 0;
}
OUTPUT:-

The parent age is:33


The child age is:19
Program no.12

Write a program to implement multiple inheritance.

#include<iostream.h>
#include<conio.h>
class sell
{
public:
double s_price;
};
class cost
{
public:
double c_price;
};
class profit:public sell,public cost
{
public:
double profit;
void setdata()
{
cout<<"Enter the selling price:"<<endl;
cin>>s_price;
cout<<"Enter the cost price:"<<endl;
cin>>c_price;
}
void getdata()
{
cout<<"The selling price is:"<<s_price<<endl;
cout<<"The cost price is:"<<c_price<<endl;
profit=s_price-c_price;
cout<<"The profit is:"<<profit<<endl;
}
};
int main()
{
clrscr();
profit p1;
p1.setdata();
cout<<endl;
p1.getdata();
getch();
return 0;
}
OUTPUT:-
Enter the selling price:
29
Enter the cost price:
22

The selling price is:29


The cost price is:22
The profit is:7
Program no.13

Write a program in C++ to implement multilevel inheritance.

#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[44];
int roll;
};
class marks:public student
{
public:
int sub1,sub2;
void getdata()
{
cout<<"Enter the name of student:"<<endl;
cin>>name;
cout<<"Enter roll no:"<<endl;
cin>>roll;
cout<<"Enter the marks of subject 1:"<<endl;
cin>>sub1;
cout<<"Enter the marks of subject 2:"<<endl;
cin>>sub2;
}
};
class result:public marks
{
int result;
public:
void calculateResult()
{
result=sub1+sub2;
}
void printResult()
{
cout<<"\n-Result--"<<endl;
cout<<"Student Name:"<<name<<endl;
cout<<"Roll No:"<<roll<<endl;
cout<<"Marks of subject 1:"<<sub1<<endl;
cout<<"Marks of subject 2:"<<sub2<<endl;
cout<<"Total marks is:"<<result<<endl;
}
};
int main()
{
clrscr();
result r1;
r1.getdata();
r1.calculateResult();
r1.printResult();
getch();
return 0;
}
OUTPUT:-
Enter the name of student:
Shravan
Enter roll no:
22
Enter the marks of subject 1:
100
Enter the marks of subject 2:
100

-Result--
Student Name:Shravan
Roll No:22
Marks of subject 1:100
Marks of subject 2:100
Total marks is:200
Program No 14

Write a Program in c++ to implement Hierarchical Inheritance.


#include<iostream.h>
#include<conio.h>
class arith
{
protected:
int a,b;
public:
void getdata()
{
cout<<"Enter two Numbers:";
cin>>a>>b;
}
};
class add:public arith
{
public:
int sum;
public:
void calsum()
{
sum=a+b;
}
void display()
{
cout<<"Addition of two no is:"<<sum<<endl;
}
};
class substract:public arith
{
public:
int sub;
public:
void calsub()
{
sub=a-b;
}
void display1()
{
cout<<"Substraction of two No is:"<<sub<<endl;
}
};
class multi:public arith
{
public:
int mult;
public:
void calmult()
{
mult=a*b;
}
void display2()
{
cout<<"Multiplication is:"<<mult;
}
};
int main()
{
clrscr();
add a1;

a1.getdata();
a1.calsum();
a1.display();

substract s1;
s1.getdata();
s1.calsub();
s1.display1();

multi m1;
m1.getdata();
m1.calmult();
m1.display2();
getch();
return 0;
}

OUTPUT:-
Enter two Numbers:20
10
Addition of two no is:30
Enter two Numbers:10
20
Substraction of two No is:-10
Enter two Numbers:10
20
Multiplication is:200
Program No 15

Write a Program in c++ to implement Hierarchical Inheritance.


#include<iostream.h>
#include<conio.h>
class GFat
{
public:
GFat()
{
cout<<"GrandFather Name is Akshay"<<endl;
}
};
class Fat:publicGFat
{
public:
Fat()
{
cout<<"FatherName is Vishal"<<endl;
}
};
class Myname
{
public:
Myname()
{
cout<<"My Name is Sachin"<<endl;
}
};
class son:publicFat,publicMyname
{
public:
son()
{
cout<<"Son Name is Shiva";
}
};
int main()
{
clrscr();
son s1;
getch();
return 0;
}

OUTPUT:-
GrandFather Name is Akshay
FatherName is Vishal
My Name is Sachin
Son Name is Shiva
Program No 16

Write a Program in c++for function overloading.

#include<iostream.h>
#include<conio.h>
class addition
{
public:
int sum(int a,int b)
{
return a+b;
}
double sum(double a,double b)
{
return a+b;
}
};
int main()
{
clrscr();
addition obj;
cout<<"Sum of two integer number is:"<<obj.sum(18,22)<<endl;
cout<<"Sum of two Double number is:"<<obj.sum(4.5,10.5);
getch();
return 0;
}

OUTPUT:-
Sum of two integer number is:40
Sum of two Double number is:15
Program No 20
Write a program in c++ for virtual base class.

#include<iostream.h>
#include<conio.h>
Class student {
Int rno;
Public:
Void getnumber() {
Cout << “Enter Roll No:”;
Cin>>rno;
}
Void putnumber() {
Cout << “\n\n\tRoll No:” << rno << “\n”;
}
};
Class test : virtual public student {
Public:
Int part1, part2;
Void getmarks() {
Cout << “Enter Marks\n”;
Cout << “Part1:”;
Cin>>part1;
Cout << “Part2:”;
Cin>>part2;
}
Void putmarks() {
H Cout << “\tMarks Obtained\n”;
Cout << “\n\tPart1:” << part1;
Cout << “\n\tPart2:” << part2;
}
};
Class sports : public virtual student {
Public:
Int score;
Void getscore() {
Cout << “Enter Sports Score:”;
Cin>>score;
}
Void putscore() {
Cout << “\n\tSports Score is:” << score;
}
};
Class result : public test, public sports {
Int total;
Public:
Void display() {
Total = part1 + part2 + score;
Putnumber();
Putmarks();
Putscore();
Cout << “\n\tTotal Score:” << total;
}
};
Void main() {
Result obj;
Clrscr();
Obj.getnumber();
Obj.getmarks();
Obj.getscore();
Obj.display();
getch();
}
OUTPUT:-
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250

You might also like