OOP
Program No 1
Write a class circle with one data member radius. Write three member functions
get_radius () to set radius value with parameter value, area () to display radius and
circum () to calculate and display the circumference of the circle.
Coding
#include<iostream>
using namespace std;
class circle
{
private:
float radius;
public:
void getradius(float rad)
{
radius=rad;
}
void displayarea()
{
float area=3.14*radius*radius;
cout<<"Area:"<<area<<endl;
}
void displaycircumference(){
float circum=2*3.14*radius;
cout<<"Cirum:"<<circum<<endl;
}
};
int main(){
circle obj;
obj.getradius(5);
obj.displayarea();
obj.displaycircumference();
return 0;
}
______________________________________________________________________________________________________________________________________________________________________
Program No 2
Write a program and input two integers in main () and pass them to the default constructor of the class. Show the result of the addition of two numbers.
Coding
#include<iostream>
using namespace std;
class sum
{
public:
int a,b,c;
void result();
sum()
{
a=5;
b=7;
}
};
void sum::result()
{
c=a+b;
cout<<"Sum="<<c;
}
int main()
{
sum s;
s.result();
return 0;
}
____________________________________________________________________________________________________________________________________________________
Program No 3
Write a class having two private variables and one member function which will returnthe area of the rectangle.
Coding
#include <iostream>
using namespace std;
class Rectangle
{
private:
float l,b;
public:
float getArea(float l, float b)
{
return l*b;
}
};
int main () {
Rectangle r1;
float l,b;
cout<<"Enter the length of the rectangle: ";
cin>>l;
cout<<"Enter the breadth of the rectangle: ";
cin>>b;
cout <<"Area of the rectangle is: "<< r1.getArea(l,b);
return 0;
}
______________________________________________________________________________________________________________________________________________________________________
Program No 4
Write a class player that contains attributes for the player’s name, average and team.
Write three functions to input, change and display these attributes. Also, write a
constructor that asks for input to initialize all the attributes
Coding
#include<iostream>
using namespace std;
class player
{
private:
string name;
float average;
char team;
public:
player(string n, float a,char t)
{
name=n;
average=a;
team=t;
}
void setname(string n)
{
name=n;
}
void setaverage(float a)
{
average=a;
}
void setteam(char t)
{
team=t;
}
string getname()
{
return name;
}
int getaverage()
{
return average;
}
char getteam()
{
return team;
}
void input(string n,float a,char t)
{
name=n;
average=a;
team=t;
}
void change(string n,float a,char t)
{
name=n;
average=a;
team=t;
}
void display()
{
cout<<"Player Name is:"<<name<<endl;
cout<<"Player Average is:"<<average<<endl;
cout<<"Player team is:"<<team<<endl;
}
};
int main()
{
string name;
float average;
char team;
cout<<"Enter name of player:"<<endl;
cin>>name;
cout<<"Enter player of average:"<<endl;
cin>>average;
cout<<"Enter Team of the player:"<<endl;
player obj(name, average, team);
obj.input(name, average, team);
obj.display();
return 0;
}
______________________________________________________________________________________________________________________________________________________________________
Program No 5
Write a program that asks for two numbers, compare them and show the maximum.
Declare a function called max_two () that compares the numbers and returns the
maximum.
Coding
#include<iostream>
using namespace std;
class marks
{
public:
int maxtwo(int a,int b)
{
if(a>b)
return a;
else
return b;
}
};
int main()
{
marks r1;
int a, b;
cin>>a;
cin>>b;
cout<<"Max="<<r1.maxtwo(a, b);
return 0;
}
______________________________________________________________________________________________________________________________________________________________________