You are on page 1of 23

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

FACULTY OF ENGINERING AND TECHNOLOGY


SCHOOL OF COMPUTING
Department of Computing Technologies

[Assignment Week 3]
21CSC101T-OBJECT ORIENTED DESIGN AND PROGRAMMING
JANUARY–MAY 2024

[Reg.no:- RA2311003010345]
[Name:- Param Maheshwari]
[Year/Section:- 1/E1]
QUESTION 1):

Create a class named 'Box' with data members such as length, breadth and height. Get the
values of length, breadth and height from the user and also calculate the volume of the
box which is to be generated in the proper format using constructors.
#include <iostream>

using namespace std;

class Box {
private:
double length;
double breadth;
double height;

public:
// Constructor
Box() {
length = 0;
breadth = 0;
height = 0;
}

// Parameterized constructor
Box(double len, double bre, double hei) {
length = len;
breadth = bre;
height = hei;
}

// Function to set dimensions


void setDimensions(double len, double bre, double hei) {
length = len;
breadth = bre;
height = hei;
}
// Function to calculate volume
double calculateVolume() {
return length * breadth * height;
}

// Function to display volume


void displayVolume() {
double volume = calculateVolume();
cout << "Volume of the box: " << volume << endl;
}
};

int main() {
double length, breadth, height;

// Getting dimensions from the user


cout << "Enter the length of the box: ";
cin >> length;
cout << "Enter the breadth of the box: ";
cin >> breadth;
cout << "Enter the height of the box: ";
cin >> height;

// Creating an instance of the Box class


Box box(length, breadth, height);

// Displaying volume
box.displayVolume();

return 0;
}

OUTPUT:
QUESTION 2: Create a C++ program to construct the circle by getting two points such as x and y using

parameterised constructor by getting the values in parameters. Also, how to access these

points to display the values in the console mode.

#include <iostream>
#include <cmath>

class Circle {
private:
double x, y; // Center of the circle

public:
// Parameterized constructor
Circle(double xCoord, double yCoord) : x(xCoord), y(yCoord) {}

// Method to access and display center coordinates


void displayCenter() {
std::cout << "Center of the circle: (" << x << ", " << y << ")" << std::endl;
}
};

int main() {
// Getting the coordinates from the user
double xCoord, yCoord;
std::cout << "Enter x-coordinate of the center: ";
std::cin >> xCoord;
std::cout << "Enter y-coordinate of the center: ";
std::cin >> yCoord;
// Constructing the circle using the parameterized constructor
Circle circle(xCoord, yCoord);

// Displaying the center coordinates


circle.displayCenter();

return 0;
}

Output:

QUESTION 3:

Create a C++ program to construct the student database using copy constructor. Get the
input such as name, roll number and fees payment details. All necessary and additional
details can also be get in the input format internally.
#include <iostream>
#include <string>

class Student {
private:
std::string name;
int rollNumber;
double feesPaid;

public:
// Parameterized constructor
Student(std::string n, int roll, double fees) : name(n), rollNumber(roll), feesPaid(fees) {}

// Copy constructor
Student(const Student &other) : name(other.name), rollNumber(other.rollNumber),
feesPaid(other.feesPaid) {}
// Method to display student details
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Roll Number: " << rollNumber << std::endl;
std::cout << "Fees Paid: " << feesPaid << std::endl;
}
};

int main() {
std::string name;
int rollNumber;
double feesPaid;

// Getting student details from the user


std::cout << "Enter student name: ";
std::getline(std::cin >> std::ws, name); // Read the name with spaces
std::cout << "Enter roll number: ";
std::cin >> rollNumber;
std::cout << "Enter fees paid: ";
std::cin >> feesPaid;

// Constructing the student object using user input


Student student1(name, rollNumber, feesPaid);

// Creating a copy of the student object using the copy constructor


Student student2 = student1;

// Displaying details of both student objects


std::cout << "\nDetails of Student 1:" << std::endl;
student1.displayDetails();

std::cout << "\nDetails of Student 2 (Copy of Student 1):" << std::endl;


student2.displayDetails();
return 0;
}

OUTPUT:

QUESTION 4:

Consider a scenario where you are designing a software system for a library management
application. The system has two main entities: Book and LibraryMember. Each Book has
attributes such as ISBN, title, and author, while each LibraryMember has attributes like
member ID, name, and contact details. Assume that both entities need to be initialized
with different sets of information. Additionally, there is a requirement to manage
dynamic memory for storing book titles. Design the class structures for Book and
LibraryMember considering constructor overloading and destructor. Implement the
necessary constructors to initialize the objects, ensuring dynamic memory management
for book titles.

#include <iostream>

#include <cstring>

class Book {
private:

char* title;

char* ISBN;

char* author;

public:

// Default constructor

Book() : title(nullptr), ISBN(nullptr), author(nullptr) {}

// Parameterized constructor for initializing with given data

Book(const char* t, const char* isbn, const char* auth)

: title(nullptr), ISBN(nullptr), author(nullptr) {

setTitle(t);

setISBN(isbn);

setAuthor(auth);

// Copy constructor

Book(const Book& other) : title(nullptr), ISBN(nullptr), author(nullptr) {

setTitle(other.title);

setISBN(other.ISBN);

setAuthor(other.author);

// Destructor

~Book() {

delete[] title;
delete[] ISBN;

delete[] author;

// Setter for title

void setTitle(const char* t) {

if (t == nullptr) return;

title = new char[strlen(t) + 1];

strcpy(title, t);

// Setter for ISBN

void setISBN(const char* isbn) {

if (isbn == nullptr) return;

ISBN = new char[strlen(isbn) + 1];

strcpy(ISBN, isbn);

// Setter for author

void setAuthor(const char* auth) {

if (auth == nullptr) return;

author = new char[strlen(auth) + 1];

strcpy(author, auth);

// Display book details

void displayDetails() const {


std::cout << "Title: " << title << std::endl;

std::cout << "ISBN: " << ISBN << std::endl;

std::cout << "Author: " << author << std::endl;

};

class LibraryMember {

private:

int memberID;

char* name;

char* contactDetails;

public:

// Default constructor

LibraryMember() : name(nullptr), contactDetails(nullptr) {}

// Parameterized constructor for initializing with given data

LibraryMember(int id, const char* n, const char* contact)

: memberID(id), name(nullptr), contactDetails(nullptr) {

setName(n);

setContactDetails(contact);

// Copy constructor

LibraryMember(const LibraryMember& other) : name(nullptr), contactDetails(nullptr) {

memberID = other.memberID;

setName(other.name);
setContactDetails(other.contactDetails);

// Destructor

~LibraryMember() {

delete[] name;

delete[] contactDetails;

// Setter for name

void setName(const char* n) {

if (n == nullptr) return;

name = new char[strlen(n) + 1];

strcpy(name, n);

// Setter for contact details

void setContactDetails(const char* contact) {

if (contact == nullptr) return;

contactDetails = new char[strlen(contact) + 1];

strcpy(contactDetails, contact);

// Display member details

void displayDetails() const {

std::cout << "Member ID: " << memberID << std::endl;

std::cout << "Name: " << name << std::endl;


std::cout << "Contact Details: " << contactDetails << std::endl;

};

int main() {

// Creating a book object

Book book1("Introduction to Algorithms", "9780262533058", "Thomas H. Cormen");

// Displaying book details

std::cout << "Book Details:" << std::endl;

book1.displayDetails();

std::cout << std::endl;

// Creating a library member object

LibraryMember member1(101, "John Doe", "johndoe@example.com");

// Displaying library member details

std::cout << "Library Member Details:" << std::endl;

member1.displayDetails();

return 0;

OUTPUT:
Question 5:

Write a C++ program to design a class 'Complex' with data members for real and

imaginary part. Provide default and parameterized constructors. Write a program

to perform arithmetic operations of two complex numbers using operator

overloading(Addition, subtraction, multiplication and division) using member functions.

#include <iostream>

class Complex {

private:

double real;

double imag;

public:

// Default constructor

Complex() : real(0.0), imag(0.0) {}


// Parameterized constructor

Complex(double r, double i) : real(r), imag(i) {}

// Addition operator overloading

Complex operator+(const Complex& other) const {

Complex result;

result.real = real + other.real;

result.imag = imag + other.imag;

return result;

// Subtraction operator overloading

Complex operator-(const Complex& other) const {

Complex result;

result.real = real - other.real;

result.imag = imag - other.imag;

return result;

// Multiplication operator overloading

Complex operator*(const Complex& other) const {

Complex result;

result.real = (real * other.real) - (imag * other.imag);

result.imag = (real * other.imag) + (imag * other.real);

return result;

}
// Division operator overloading

Complex operator/(const Complex& other) const {

Complex result;

double denominator = (other.real * other.real) + (other.imag * other.imag);

result.real = ((real * other.real) + (imag * other.imag)) / denominator;

result.imag = ((imag * other.real) - (real * other.imag)) / denominator;

return result;

// Display complex number

void display() const {

if (imag < 0)

std::cout << real << " - " << -imag << "i" << std::endl;

else

std::cout << real << " + " << imag << "i" << std::endl;

};

int main() {

// Creating complex numbers

Complex num1(3.0, 4.0);

Complex num2(1.0, -2.0);

// Performing arithmetic operations

Complex sum = num1 + num2;

Complex diff = num1 - num2;

Complex prod = num1 * num2;


Complex div = num1 / num2;

// Displaying results

std::cout << "Sum: ";

sum.display();

std::cout << "Difference: ";

diff.display();

std::cout << "Product: ";

prod.display();

std::cout << "Division: ";

div.display();

return 0;

OUTPUT:

Question 6:

Elavenil is the working in Survey of India, The National Survey and Mapping

Organization of the country under the Department of Science & Technology. Now

Elavenil has been assigned the task of Collecting the Area and Density Information of all

the states of India from the local authorities of the respective states and to consolidate in a
common portal of Government of INDIA. Since the task assigned to her is highly

complicated in nature she is seeking your help. Can you help her? Use the Concept of

Constructor Overloading to complete the task.

#include <iostream>

#include <string>

class StateInformation {

private:

std::string stateName;

double area;

double populationDensity;

public:

// Default constructor

StateInformation() : area(500000.0), populationDensity(500.0) {}

// Constructor with state name

StateInformation(const std::string& name) : stateName(name), area(0.0), populationDensity(0.0) {}

// Constructor with state name, area, and population density

StateInformation(const std::string& name, double a, double pd) : stateName(name), area(a),


populationDensity(pd) {}

// Setter for state name

void setStateName(const std::string& name) {

stateName = name;

}
// Setter for area

void setArea(double a) {

area = a;

// Setter for population density

void setPopulationDensity(double pd) {

populationDensity = pd;

// Display state information

void displayInformation() const {

std::cout << "State: " << stateName << std::endl;

std::cout << "Area: " << area << " square kilometers" << std::endl;

std::cout << "Population Density: " << populationDensity << " people per square kilometer" <<
std::endl;

};

int main() {

// Creating an object using default constructor

StateInformation state1("Rajasthan", 342239, 700);

state1.displayInformation();

// Creating an object with state name

StateInformation state2("Maharashtra",307713, 1000);


state2.displayInformation();

// Creating an object with state name, area, and population density

StateInformation state3("Uttar Pradesh", 243290, 828);

state3.displayInformation();

return 0;

return 0;

}Output:

Question 7:

Provide an example where a destructor is used to release dynamically allocated memory.

#include <iostream>

class DynamicArray {

private:

int* data;

int size;
public:

// Constructor to initialize the array size and allocate memory

DynamicArray(int s) : size(s) {

data = new int[size];

// Destructor to release dynamically allocated memory

~DynamicArray() {

delete[] data;

// Method to set the value at a specific index

void setValue(int index, int value) {

if (index >= 0 && index < size)

data[index] = value;

else

std::cout << "Index out of bounds!" << std::endl;

// Method to get the value at a specific index

int getValue(int index) const {

if (index >= 0 && index < size)

return data[index];

else {

std::cout << "Index out of bounds!" << std::endl;


return -1; // Returning a default value for simplicity

};

int main() {

// Creating a DynamicArray object with size 5

DynamicArray arr(5);

// Setting values

arr.setValue(0, 10);

arr.setValue(1, 20);

arr.setValue(2, 30);

arr.setValue(3, 40);

arr.setValue(4, 50);

// Getting and displaying values

for (int i = 0; i < 5; ++i) {

std::cout << "Value at index " << i << ": " << arr.getValue(i) << std::endl;

// No need to manually release memory, the destructor will take care of it when 'arr' goes out of scope

return 0;

Output:
Question 8:

Sequence diagram exercise: Poker use case, Start New Game Round The scenario begins

when the player chooses to start a new round in the UI. The UI asks whether any new

players want to join the round; if so, the new players are added using the UI. All players'

hands are emptied into the deck, which is then shuffled. The player left of the dealer

supplies an ante bet of the proper amount. Next each player is dealt a hand of two cards

from the deck in a round-robin fashion; one card to each player, then the second card. If

the player left of the dealer doesn't have enough money to ante, he/she is removed from

the game, and the next player supplies the ante. If that player also cannot afford the ante,

this cycle continues until such a player is found or all players are removed.

You might also like