Professional Documents
Culture Documents
Apse Programs
Apse Programs
Submitted by:
Name:
Surabhi Agarwal
Roll no.:
1130294
Section: E5
INDEX
function by reading and printing the student roll no. , name and also write
overloading.
C++ program to implement THIS pointer
C++ program to implement multiple and multilevel inheritance.
C++ program to achieve hybrid inheritance.
C++ program to achieve late binding through the virtual functions.
C++ program to apply the concept of inheritance to estimate the bills of
Program 1:
Write a c++ program to implement structures of class and member
function by reading and printing the student roll no. , name and also write
the function to assign the grade of student.
#include<iostream>
using namespace std;
class student
{
char name[30];
int marks;
public:
void getdata();
void show();
void grade();
};
void student:: getdata()
{
cout<<"\nEnter name and marks\n";
cin>>name>>marks;
}
void student::show()
{
cout<<"\nName: "<<name<<endl;
cout<<"Marks: "<<marks<<endl;
}
void student::grade()
{
int m=marks;
if(m>=85 && m<=100)
cout<<"A+"<<endl;
else if(m>=75&&m<85)
cout<<"A"<<endl;
else if (m>=65 && m<75)
cout<<"B"<<endl;
else if(m>=50 && m<65)
cout<<"C"<<endl;
else if(m>=40 && m<50)
cout<<"D"<<endl;
else if(m<40)
cout<<"Fail"<<endl;
else if(m<0&&m>100)
cout<<"Invalid marks"<<endl;
}
int main ()
{
student s;
s.getdata();
s.show();
s.grade();
return 0;
}
Program 2:
C++ program to demonstrate the working of friend function.
#include<iostream>
using namespace std;
class test
{
int a,b;
public:
void get(int x ,int y){a=x;b=y;}
friend float average(test);
};
float average(test t)
{
return float(t.a+t.b)/2;
}
int main()
{
test t;
t.get(4,5);
cout<<"average is:"<<average (t);
return 0;
}
Program 3:
C++ program to multiply two complex numbers using operator
overloading.
#include<iostream>
using namespace std;
class complex
{
float r,i;
public:
complex(){};
complex(float x, float y){r=x; i=y;}
complex operator +(complex);
complex operator *(complex);
void show();
};
complex complex :: operator +(complex c2)
{
complex c3;
c3.r=c2.r+r;
c3.i=c2.i+i;
return c3;
}
complex complex :: operator *(complex c2)
{
complex c3;
c3.r=(r*c2.r)-(i*c2.i);
c3.i=(r*c2.i)+(i*c2.i);
return c3;
}
void complex :: show()
{
if (i>=0)
cout <<r<<"+j"<<i<<endl;
else
cout<<r<<"-j"<<-1*i<<endl;
}
int main()
{
complex c1(5,7.5);
complex c2(5.6,7.9);
complex c3;
cout<<"addition:"<<endl;
c3=c1+c2;
c3.show();
cout<<"multiplication:"<<endl;
c3=c1*c2;
c3.show();
return 0;
}
Program 4:
C++ program to implement THIS pointer
#include<iostream>
#include<string.h>
using namespace std;
class person
{
char name[50];
int age;
public:
void getdata(int a, char*s);
person greater(person);
void show();
};
void person :: getdata(int a, char*s)
{
strcpy(name,s);
age=a;
}
void person :: show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
person person :: greater(person x)
{
if (x.age> this-> age)
return x;
}
int main()
{
person p1, p2, p3;
p1.getdata(25,"abc");
p2.getdata(27,"xyz");
p3=p1.greater(p2);
p3.show();
return 0;
}
Program 5:
C++ program to implement multiple and multilevel inheritance.
#include<iostream>
using namespace std;
class student
{
char name[30];
int r;
public:
void getdata()
{
cout<<"enter name and roll no."<<endl;
cin>>name;
cin>>r;
}
void showdata()
{
cout<<"name:"<<name<<endl;
cout<<"roll no."<<r<<endl;
}
};
class test:virtual public student
{
protected:
int s1, s2;
public:
void getmarks()
{
cout<<"enter subject 1 and 2 marks"<<endl;
cin>>s1>>s2;
}
void showmarks()
{
cout<<"subject 1: "<<s1<<endl;
cout<<"subject 2: "<<s2<<endl;
}
};
class sports:virtual public student
{
protected:
int m;
public:
void gets_marks()
{
cout<<"enter sports marks"<<endl;
cin>>m;
}
void shows_marks()
{
cout<<"sports marks: "<<m<<endl;
}
};
class result: public test, public sports
{
int total;
public:
void display();
};
void result:: display()
{
getdata();
getmarks();
gets_marks();
showdata();
showmarks();
shows_marks();
total=s1+s2+m;
cout<<"total:"<<total<<endl;
}
int main()
{
result s;
s.display();
return 0;
}
Program 6:
C++ program to achieve hybrid inheritance.
#include <iostream>
using namespace std;
class Number
{
private:
int num;
public:
void getNumber(void)
{
cout << "Enter an integer number: ";
cin >> num;
}
//to return num
int returnNumber(void)
{ return num; }
};
public:
int getCube(void)
{
int num,cube;
num=returnNumber(); //get number from class Number
cube=num*num*num;
return cube;
}
};
int main()
{
Square objS;
Cube objC;
int sqr,cube;
objS.getNumber();
sqr =objS.getSquare();
cout << "Square of "<< objS.returnNumber() << " is: " << sqr << endl;
objC.getNumber();
cube=objC.getCube();
cout << "Cube of "<< objS.returnNumber() << " is: " << cube << endl;
return 0;
}
Program 7:
C++ program to achieve late binding through the virtual functions.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
std::cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
std::cout << "Derived Class";
}
};
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
Program 8:
C++ program to apply the concept of inheritance to estimate the bills of
various consumers subject to two part tariff.
using namespace std;
#include <iostream>
#include <conio.h>
class account
{
char cust_name[20];
int acc_no;
char acc_type[20];
public:
void get_accinfo()
{
cout<<"\n\nEnter Customer Name :- ";
cin>>cust_name;
cout<<"Enter Account Number :- ";
cin>>acc_no;
cout<<"Enter Account Type :- ";
cin>>acc_type;
}
void display_accinfo()
{
cout<<"\n\nCustomer Name :- "<<cust_name;
cout<<"\nAccount Number :- "<<acc_no;
cout<<"\nAccount Type :- "<<acc_type;
}
};
int choice;
if(type=='s' || type=='S')
{
s1.get_accinfo();
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<"6) Choose Your choice:-";
cin>>choice;
switch(choice)
{
case 1 : s1.deposit_savbal();
getch();
break;
case 2 : s1.withdraw_savbal();
getch();
break;
case 3 : s1.disp_savbal();
getch();
break;
case 4 : s1.display_accinfo();
s1.disp_savbal();
getch();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
else
{
{
c1.get_accinfo();
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<"6) Choose Your choice:-";
cin>>choice;
switch(choice)
{
case 1 : c1.deposit_currbal();
getch();
break;
case 2 : c1.withdraw_currbal();
getch();
break;
case 3 : c1.disp_currbal();
getch();
break;
case 4 : c1.display_accinfo();
c1.disp_currbal();
getch();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
end:
return 0;
}}
Program 9:
C++ program to simulate game of chances using OOP paradigm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rand_num()
{
return ( 1 + (rand()%6) );
}
int main( void )
{
int D1, D2, score, point;
int D3, D4, score_n = 0;
D1 = rand_num();
D2 = rand_num();
score = D1 + D2;
printf( "You rolled %d + %d = %d", D1, D2, score );
if ( score == 7 || score == 11 )
printf( "\n\nYou win!");
else
{
point = score;
printf( "\n\nYou must get %d to win", point);
do
{
D3 = rand_num();
D4 = rand_num();
score_n = D3 + D4;
printf( "\n\nYou rolled %d + %d = %d", D3, D4, score_n );
if ( score_n == 7 )
printf( "\n\nYou lose!" );
if ( score_n == point )
printf( "\n\nYou win!" );
}
return 0;
}
Program 10:
C++ program to implement function templates to find out the roots of
second order quadratic equation.
using namespace std;
#include<iostream>
#include<math.h>
template<class T>
void roots(T a, T b , T c )
{float d;
d=pow(b,2)-4*a*c;
if(d==0)
{cout<<"\n\nroots are real and equal\n";
cout<<"Root is"<<(-1)*b/(2*a);
}
else if(d>0)
{cout<<"\n\nroots are real and distinct\n";
cout<<"Root 1 is\t"<<((-1)*b+sqrt(d))/(2*a);
cout<<"Root 2 is\t"<<((-1)*b-sqrt(d))/(2*a);
}
else
{cout<<"\n\nRoots are complex\n";
cout<<"Real part of root is\t"<<((-1)*b)/(2*a);
cout<<"\nImaginary part of root is\t"<<sqrt((-1)*d)/(2*a);
}
}
int main()
{roots(1,2,3);
roots(1.6,5.8,52.6);
return 0;
}