0% found this document useful (0 votes)
44 views11 pages

Week 2OODP

The document contains examples of creating classes in C++ and using them to define objects and call methods. Several classes are defined like Student, Circle, StringManipulator, Triangle, Average, Room and Car. Methods like calculateArea(), calculatePerimeter(), get_Str(), print_Str() are defined and called on objects.

Uploaded by

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

Week 2OODP

The document contains examples of creating classes in C++ and using them to define objects and call methods. Several classes are defined like Student, Circle, StringManipulator, Triangle, Average, Room and Car. Methods like calculateArea(), calculatePerimeter(), get_Str(), print_Str() are defined and called on objects.

Uploaded by

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

Week 2 – OODP

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;

cout << "Student Name: " << studentObject.name << endl;


cout << "Roll Number: " << studentObject.roll_no << endl;
return 0;
}

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

void print_Str() const {


cout << "Stored String: " << storedString << endl;
}
};

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

void print_Str() const {


cout << "Stored String: " << storedString << endl;
cout << "Upper Case: " << convertCase(storedString, ::toupper) << endl;
cout << "Lower Case: " << convertCase(storedString, ::tolower) << endl;
}

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;

Triangle(double s1, double s2, double s3) : side1(s1), side2(s2), side3(s3) {}

double calculatePerimeter() const {


return side1 + side2 + side3;
}

double calculateArea() const {


double s = calculatePerimeter() / 2;
return sqrt(s * (s - side1) * (s - side2) * (s - 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;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

double average = (num1 + num2 + num3) / 3.0;

cout << "Average: " << average << endl;


}
};

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;

cout << "Room 1:" << endl;


cout << "Area: " << r1.calculateArea() << "\nVolume: " << r1.calculateVolume() << endl;

cout << "Room 2:" << endl;


cout << "Area: " << r2.calculateArea() << "\nVolume: " << r2.calculateVolume() << endl;

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>

using namespace std;

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;

cout << "Car Attributes:" << endl;


carObject.printAttributes();

return 0;
}

Output:
Car Attributes:
Make: Toyota
Model: Camry
Year: 2022
Price: $25000

You might also like