Week 2OODP
Week 2OODP
RA2211047010063
Santhosh MM
B.Tech AI
A Section
1. Create a class named 'Student' with a string variable 'name' and an integer variable
'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an
object of the class Student.
Code:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int roll_no;
};
int main() {
Student studentObject;
studentObject.name = "John";
studentObject.roll_no = 2;
Output:
Student Name: John
Roll Number: 2
2. Define a class named Circle which can be constructed by a radius. TheCircle class has
two methods for computing perimeter and area, respectively
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Circle {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculatePerimeter() const {
return 2 * M_PI * radius;
}
double calculateArea() const {
return M_PI * radius * radius;
}
};
int main() {
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
Circle circle(radius);
cout << "Perimeter of the circle: " << circle.calculatePerimeter() << endl;
cout << "Area of the circle: " << circle.calculateArea() << endl;
return 0;}
Input:
Enter the radius of the circle: 5
Output:
Perimeter of the circle: 31.4159
Area of the circle: 78.5398
3. Write a C++ class which has two funtionsget_Str and print_Str. get_Str accept a string
Code:
#include <iostream>
#include <string>
using namespace std;
class StringManipulator {
private:
string storedString;
public:
void get_Str() {
cout << "Enter a string: ";
getline(cin, storedString);
}
int main() {
StringManipulator stringObject;
stringObject.get_Str();
stringObject.print_Str();
return 0;
}
Input:
Enter a string: Hello
Output:
Stored String: Hello
4. from the user and print_Str print the string in upper case and lower case.
Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class StringManipulator {
private:
string storedString;
public:
void get_Str() {
cout << "Enter a string: ";
getline(cin, storedString);
}
private:
string convertCase(const string& str, int (*conversionFunc)(int)) const {
string result = str;
transform(result.begin(), result.end(), result.begin(), conversionFunc);
return result;
}
};
int main() {
StringManipulator stringObject;
stringObject.get_Str();
stringObject.print_Str();
return 0;
}
Input:
Enter a string: hello
Output:
Stored String: hello
Upper Case: HELLO
Lower Case: hello
5. 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.
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Triangle {
public:
double side1;
double side2;
double side3;
int main() {
Triangle triangle(3, 4, 5);
cout << "Triangle with sides: " << triangle.side1 << ", "
<< triangle.side2 << ", " << triangle.side3 << endl;
cout << "Perimeter: " << triangle.calculatePerimeter() << endl;
cout << "Area: " << triangle.calculateArea() << endl;
return 0;
}
Output;
Triangle with sides: 3, 4, 5
Perimeter: 12
Area: 6
6. 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.
Code:
#include <iostream>
using namespace std;
class Average {
public:
static void calculateAndPrintAverage() {
double num1, num2, num3;
int main() {
Average::calculateAndPrintAverage();
return 0;
}
Input:
Enter three numbers: 4 5 6
Output:
Average: 5
7. Develop a program of class Room with attributes length, breadth and height and
itsobject room1 and room2 to calculate the area and volume of a room using function.
Code:
#include <iostream>
using namespace std;
class Room {
public:
double l, b, h;
double calculateArea() {
return 2 * (l * b + b * h + h * l);
}
double calculateVolume() {
return l * b * h;
}
};
int main() {
Room r1, r2;
cout << "Room 1 Dimensions: " << endl;
cin >> r1.l >> r1.b >> r1.h;
cout << "Room 2 Dimensions: " << endl;
cin >> r2.l >> r2.b >> r2.h;
return 0;
}
Input:
Room 1 Dimensions:
456
Room 2 Dimensions:
789
Output:
Room 1:
Area: 148
Volume: 120
Room 2:
Area: 382
Volume: 504
8. Design a program of class Car with some attributes and its object to print itsattributes.
Code:
#include <iostream>
#include <string>
class Car {
public:
string make;
string model;
int year;
double price;
void printAttributes() {
cout << "Make: " << make << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
cout << "Price: $" << price << endl;
}
};
int main() {
Car carObject;
carObject.make = "Toyota";
carObject.model = "Camry";
carObject.year = 2022;
carObject.price = 25000.0;
return 0;
}
Output:
Car Attributes:
Make: Toyota
Model: Camry
Year: 2022
Price: $25000