Oodp Assignment - 3
Oodp Assignment - 3
Oodp Assignment - 3
#include <iostream>
class student{
public:
string name;
int rollno;
void inputdetail(){
//cout<<"Enter Name : ";
// getline(cin,name);
//cout<<"Enter RollNo. : ";
//cin>>rollno;
name="John";
rollno=2;
}
void printdetail(){
cout<<"Student's Name : "<<name<<endl;
cout<<"Student's RollNo. : "<<rollno<<endl;
}
};
int main()
{
student s;
s.inputdetail();
s.printdetail();
return 0;
}
Write a C++ program to illustrate the concept of class and object creation. (Ask
students to create a class, methods and invoke them inside main method).
#include <iostream>
class car{
public:
string name;
string carno;
void getdetails()
{
cout<<"name: ";
cin>>name;
cout<<"car no.: ";
cin>>carno;
}
void printdetails()
{
cout<<name<<endl;
cout<<carno<<endl;
}
};
int main()
{
car c;
c.getdetails();
c.printdetails();
return 0;
}
Define a class named Circle which can be constructed by a radius. The Circle class has two
methods for computing perimeter and area, respectively.
#include <iostream>
#include <math.h>
class circle{
public:
int radius;
float area;
void perimeter(){
std::cout << "Enter Radius : " ;
std::cin >> radius;
area=2*3.14*radius;
std::cout << "Perimeter : " <<area<< std::endl;
}
void Area(){
std::cout << "Enter Radius : ";
std::cin >> radius;
area=3.14*pow(radius,2);
std::cout << "Area : " <<area<< std::endl;
}
};
int main()
{
circle c;
c.perimeter();
c.Area();
return 0;
}
Write a C++ class which has two funtions get_Str and print_Str. get_Str accept a string
from the user and print_Str print the string in upper case and lower case.
#include<iostream>
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
class upperandlowerstr{
private: string str;
public:
void get_Str(){
cout<<"Enter a string: ";
getline(cin,str);
}
void print_Str(){cout<<"UPPER CASE: "<<endl;
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout<<str<<endl;
transform(str.begin(), str.end(), str.begin(), ::tolower);
cout<<"LOWER CASE: "<<endl;
cout<<str<<endl;
}
};
int main(){class upperandlowerstr s;
s.get_Str();
s.print_Str();
return 0;
}
Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5
units by creating a class named ;Triangle; with a function to print the area and perimeter.
#include <iostream>
#include <math.h>
class triangle{
public:
int a=3,b=4,c=5,p,A,s;
void perimeter(){
p=a+b+c;
cout << "Perimeter : " <<p<< endl;
}
void Area(){
s=p/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
};
int main()
{
triangle t;
t.perimeter();
t.Area();
return 0;
}
Print the average of three numbers entered by the user by creating a class named ;Average
having a function to calculate and print the average without creating any object of the
Average class.
#include <iostream>
void printavg(){
cout<<"Average : "<<(a+b+c)/3;
}
};
int main()
{
Average a;
a.average();
a.printavg();
return 0;
}
Develop a program of class Room with attributes length, breadth and height and its object
room1 and room2 to calculate the area and volume of a room using function.
#include <iostream>
class Room{
public:
int a,b,c,area,vol;
void room1(){
cout<<"Enter Three attribute. for room 1 :";
cin>>a>>b>>c;
area=a*b;
cout<<"area = "<<area<<endl;
vol=a*b*c;
cout<<"vol = "<<vol<<endl;
}
void room2(){
cout<<"Enter Three attribute. for room 2:";
cin>>a>>b>>c;
area=a*b;
cout<<"area = "<<area<<endl;
vol=a*b*c;
cout<<"vol = "<<vol<<endl;
}
};
int main()
{
Room r;
r.room1();
r.room2();
return 0;
}
Design a program of class Car with some attributes and its object to print its attributes.
#include <iostream>
class car{
public:
string name;
string carno;
void getdetails()
{
cout<<"name: ";
cin>>name;
cout<<"car no.: ";
cin>>carno;
}
void printdetails()
{
cout<<name<<endl;
cout<<carno<<endl;
}
};
int main()
{
car c;
c.getdetails();
c.printdetails();
return 0;
}
Add two distances in inch-feet by creating a class named AddDistance..
#include <iostream>
class AddDistance {
private:
int feet;
int inch;
public:
void setDistance();
void getDistance();
AddDistance addDistance( AddDistance d );
};
void AddDistance::setDistance() {
cout << " feet: "; cin >> feet;
cout << "inches: "; cin >> inch;
}
void AddDistance::getDistance() {
cout << "feet: " << feet;
cout << " inches: " << inch;
}
int main() {
AddDistance d1, d2, d3;
d3 = d1.addDistance(d2);
15. C++ Program to find Factorial by defining Function outside of the class.
#include<iostream>
using namespace std;
class Factorial_Number
{
private:
int n,n1,f=1;
public:
void input();
void calc();
void display();
};
void Factorial_Number::input()
{
cout<<"Please Enter a no.:"<<endl;
cin>>n;
}
void Factorial_Number::calc()
{
n1=n;
if(n==0||n==1)
cout<<" Factorial of "<<n<<" is 1"<<endl;
else
{
while(n>0)
{
f=f*n;
n--;
}
}
}
void Factorial_Number::display()
{
cout<<" The Factorial of "<<n1<<" is "<<f;
}
int main ()
{
Factorial_Number object;
object.input();
object.calc();
object.display();
}