0% found this document useful (0 votes)
28 views

Oodp Assignment - 3

The document contains 15 programming problems involving classes and objects in C++, including problems to calculate the area and perimeter of shapes, find the average of numbers, add distances, and calculate factorials. Students are asked to write C++ programs that define classes with methods to input and output data, perform calculations, and invoke class methods from a main function. The programs demonstrate concepts like creating objects, defining class methods, and using classes and objects to model real-world entities and solve problems.

Uploaded by

Bhuvan Beera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Oodp Assignment - 3

The document contains 15 programming problems involving classes and objects in C++, including problems to calculate the area and perimeter of shapes, find the average of numbers, add distances, and calculate factorials. Students are asked to write C++ programs that define classes with methods to input and output data, perform calculations, and invoke class methods from a main function. The programs demonstrate concepts like creating objects, defining class methods, and using classes and objects to model real-world entities and solve problems.

Uploaded by

Bhuvan Beera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

OODP ASSIGNMENT - 3

Reg no. RA211128010146


Parakh Tyagi
Q1
02/09/2022

#include <iostream>

using namespace std;

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>

using namespace std;

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>

using namespace std;

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>

using namespace std;

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));

cout << "Area : " <<A<<endl;


}

};

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>

using namespace std;


class Average{
public:
int a,b,c;
void average(){
cout<<"Enter Three no. :";
cin>>a>>b>>c;

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>

using namespace std;

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>

using namespace std;

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>

using namespace std;

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;
}

AddDistance AddDistance::addDistance( AddDistance d ) {


AddDistance dist;

dist.feet = feet + d.feet;


dist.inch = inch + d.inch;

if(dist.inch >= 12) {


dist.feet++;
dist.inch -= 12;
}
return dist;
}

int main() {
AddDistance d1, d2, d3;

cout << "Enter length of Distance 1: " << endl;


d1.setDistance();
cout << "Enter length of Distance 2: " << endl;
d2.setDistance();

d3 = d1.addDistance(d2);

cout << "Sum of Distance 1 and Distance 2:" << endl;


d3.getDistance();
return 0;
}

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();
}

You might also like