C Final
C Final
Output:-
12) create a class ‘airth’ which consists of a float and an integer
variable . write member function add(),mul(),div() to perform addition
multiplication and subtraction and division respectively. Write
member function to get and displays values ?
#include<iostream>
#include<conio.h>
#include<process.h>
Using namespace std;
Class airth
{
Private:
{
Int a;
Float b;
Public:
Void getdata()
{
Cin>>a>>b;
}
Void showdata()
{
Cout<<”\n a= “,<<a<<endl;
Cout<<”\n b= “ <<b<<endl;
}
Float add()
{
Return a+b;
}
Float sub()
{
Return a-b;
}
Float sub()
{
Return a*b;
}
Float mul()
{
Return a*b;
}
Float div()
{
Return a/b;
}
};
Int main()
{
Cout<<”\n arithmetic operations “ :
Cout<<”\n addition “;
Cout<<”\n subtraction “;
Cout<<”\n multiplication “;
Cout<<”\n division “;
Cout<<”/n exit”;
Cout<<”\n enter your choice “;
Cin>>choice;
Switch(choice)
{
Case 1:
Cout<<” enter two numbers : “;
A1.getdata();
A1.showdata();
Cout<<endl;
Cout<<” a+ b= “<<a1.add()<<<end;
Break;
Case 2:
Cout<<” enter two number : “;
A1.getdata();
A1.showdata();
Cout<< endl;
Cout<<” a - b= “<<a1.sub()<<<end;
Break;
Case 3:
Cout<<” enter two numbers “ ;
A1.getdata();
A1.showdata();
Cout<<endl;
Cout<<” a* b= “<<a1.mul()<<<end;
Break;
Case 4:
Cout<<” enter two numbers “;
A1.getdata();
A1,showdata();
Cout<<endl;
Cout<<” a/ b= “<<a1.div()<<<end;
Break;
Case 5:
Exit(0);
Default:
Cout<<” invalid choice !! thank you “;
}
}
}
Output:-
13) create two classes to represent the savings bank account and
current account for a customer with ‘customer name, accno’ and
‘balance’. Write friend function total balance() to find the total
balance of both the accounts ?
#include<iostream>
#include<string.h>
using namespace std;
class currentaccount;
class savingaccount
{
private:
string customername;
int accountnumber;
double balance;
public:
savingaccount(const std::string&name , int accno , double
initialbalance):customername(name), accountnumber(accno),balance(initialbalance) {}
friend double totalbalance(const savingaccount & savinng , const currentaccount & current);
};
class currentaccount
{
private:
string customername;
int accountnumber;
double balance;
public:
currentaccount(const std:: string &name , int accno , double
initialbalance):customername(name), accountnumber(accno), balance(initialbalance) {}
friend double totalbalance(const savingaccount & saving , const currentaccount & current);
};
double totalbalance(const savingaccount & saving , const currentaccount & current)
{
return saving.balance+current.balance;
}
int main()
{
savingaccount saving("priyanka", 13229 , 10000.0);
currentaccount current("priyanka ",2345 , 15000.0);
double total=totalbalance(saving, current);
cout<<" total balance of account :"<<total<<endl;
return 0;
}
Output:-
14) create a class called “student” containing ‘name’ ‘regno’ and ‘total_marks’ ,
write a program to accept the information of ‘n’ students in a class and print the
student merit list.
#include<iostream>
Using namespace std;
Class student
{
Int regno;
Char name[30];
Float marks;
Public:
Void getdata();
Void putdata();
};
Void student :: getdata()
{
Cout<<” enter registration number “ << endl;
Cin>>regno;
Cout<<” enter the student name “<< endl;
Cin>>name;
Cout<<” enter the student marks “ <<marks;
Cin>>marks;
}
Void student :: putdata()
{
Cout<<regno<<” /n “;
Cout<<name<<”/n “;
Cout<<marks<<” /n”;
}
Int main()
{
Student st[30] ;
Int n, I;
Cout<<” enter the number of student “ ;
Cin>> n;
for( i=0; i<n; i++ )
St [i] .getdata();
Cout<<” student data “ << endl;
for(i=0; i<n; i++)
st [i] .putdata();
Return 0;
} output :-
15) write a program to create an overloaded function volume() to
calculate volume of sphere , cylinder and cuboid.
#include<iostream>
Using namespace std;
Float volume ( int, int );
Float volume ( float );
Int volume ( int )
Int main()
{
Int radius, hight , side;
float radius1;
Cout<<” enter the value of radius “<<endl;
Cin>>radius;
Cout<<” enter the value of hight “<<endl;
Cin>>hight;
Cout<<” enter the value of side “<<endl;
Cin>>side;
Cout<<” enter the value of radius1 “<<endl;
Cin>>radius1;
Cout<<” The volume of cylinder “ <<volume( radius , hight )<<endl;
Cout<<” The volume of sphher “ <<volume( radius1 )<<endl;
Cout<<” The volume of cubeoid “ <<volume( side )<<endl;
return 0;
}
float volume( int radius , int hight);
{
return (3.14*radius*radius *hight);
}
float volume(float radius1)
{
return((4*3*3.14 *radius1 *radius1 8 radius1);
}
Int volume( int side )
{
return(side * side * side);
}
}
Output :-
16) write a program to create a class ‘ box’ . write overloaded
constructor to initialize the dimension of box.
#include<iostream>
Using namespace std;
Class Box
{
Private:
Int length;
Int bridth ;
Int hight ;
Public:
Box()
{
length=20;
bridth=30;
hight=40;
}
Box( int l , int b , int h )
{
length= l;
bridth = b;
hight =h;
}
Box( int len )
{
length = len;
bridth = 15;
hight=30;
}
Int dimensionbox()
{
return length * bridth * higth ;
}
};
Box box1 , box2( 50, 30 , 40 ) , box3(30);
Cout<<” when no argument pass “<< endl;
Cin>>” dimension of box “ <<box1.dimensionbox() ;
Cout << “ when (50 , 30, 40 ) is passed “ << endl ;
Cin>>” dimension of box “<<box2.dimensionbox();
Cout<<” when bridth and hight is fixd (30) passed “ <<endl;
Cin>> “ dimension of box “ <<box3.dimensionbox();
Cout<<endl;
return 0;
}
Output:-
17) write a program to create a class ‘student’ and write member function to get
and display student details. Create a class ‘ test’ that inherits ‘student’ write
member function in ‘test’ class to accept marks of a student in ‘five’ subject and
display.
#include<iostream>
Using namespace std;
Class student
{
Public:
Char name_of_student[30];
Int Roll_number;
Char Section;
Void getdata()
{
Cout<<” Enter the name of student “<<endl;
Cin>>name_of_student;
Cout<<” Enter the Roll no of student “<<endl;
Cin>>Roll_number;
Cout<<” Enter the section of student “<<endl;
Cin>>section;
}
Void putdata()
{
Cout<<”Name of student “<<name;
Cout<<”Roll number “<<Roll_number;
Cout<<”section”<<section;
}
};
Class test : public student
{
Public:
Float CPP , DSA , SAD, EVS, Hindi;
Void getdata()
{
Cout<<” Enter the obtained marks of student in five subject “<<endl;
Cout<<” Marks obtained in C++”;
Cin>>CPP;
Cout<<” Marks obtained in data structure “ <<endl;
Cin>>DSA;
Cout<<” Marks obtained in system analysis and design “ <<endl;
Cin>>SAD;
Cout<<” Marks obtained in Environmental Studies “ <<endl;
Cin>>EVS;
Cout<<”Marks obtained in Hindi “ <<endl;
Cin>>Hindi;
}
Void putdata()
{
Cout<<”Result of student “ <<endl;
Cout<<” this is detaild marks in each subject :: “ <<endl;
cout<<"C++"<<"---> "<<CPP<<endl;
cout<<"DSA"<<"---> "<<DSA<<endl;
cout<<"SAD"<<"---> "<<SAD<<endl;
cout<<"EVS"<<"---> "<<EVS<<endl;
cout<<"Hindi"<<"---> "<<Hindi<<endl;
}
};
int main()
{ student s;
test t;
s.getdata();
t.getdata();
s.putdata();
t.putdata();
return 0; }
Output:-
18) create a class ‘shape’ which consists of two virtual functions cal_area () and
call_peri() to calculate area and parameter of various figures. Derive three
classes ‘square’ ,’rectangle’ and ‘triangle’ from the the class shape and calculate
area and perimeter of each class separately and display the result .
#include<iostream>
#include<cmath>
using namespace std;
class shape
{
public:
virtual double cal_area()=0;
virtual double cal_peri()=0;
};
class square :public shape
{
double side;
public:
square(double s):side(s) {}
double cal_area()
{
return (side*side);
}
double cal_peri()
{
return (4*side);
}
};
class recatangle : public shape
{
private:
double length;
double width;
public:
recatangle (double l , double w) : length(l) , width(w) {}
double cal_area()
{
return (length*width);
}
double cal_peri()
{
return 2*(length + width );
}
};
class triangle : public shape
{
private:
double side1;
double side2;
double side3;
public:
triangle (double s1, double s2, double s3):side1(s1) , side2(s2) , side3(s3) {}
double cal_area()
{
double s=(side1+side2+side3)/2;
return sqrt (s*(s-side1) *(s-side2) *(s- side3));
}
double cal_peri()
{
return side1+side2+side3;
}
};
int main()
{
square square(4);
recatangle recatangle(5,6);
triangle triangle(3,4,5);
cout<<" Square--->> "<<endl;
cout<<endl;
cout<<" area of square "<<square.cal_area()<<endl;
cout<<endl;
cout<<"perimeter of square "<<square.cal_peri()<<endl;
cout<<endl;
cout<<" Recatangle-->>"<<endl;
cout<<endl;
cout<<" area of recatangle "<<recatangle.cal_area()<<endl;
cout<<endl;
cout<<" perimeter of recatangle "<<recatangle.cal_peri()<<endl;
cout<<endl;
cout<<" Triangle -->>"<<endl;
cout<<endl;
cout<<" area of triangle "<< triangle.cal_area()<<endl;
cout<<endl;
cout<<" perimeter of triangle "<<triangle.cal_peri()<<endl;
return 0;
}
Output:-