Professional Documents
Culture Documents
[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>
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;
}
int main() {
double 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
#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) {}
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);
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;
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
setTitle(t);
setISBN(isbn);
setAuthor(auth);
// Copy constructor
setTitle(other.title);
setISBN(other.ISBN);
setAuthor(other.author);
// Destructor
~Book() {
delete[] title;
delete[] ISBN;
delete[] author;
if (t == nullptr) return;
strcpy(title, t);
strcpy(ISBN, isbn);
strcpy(author, auth);
};
class LibraryMember {
private:
int memberID;
char* name;
char* contactDetails;
public:
// Default constructor
setName(n);
setContactDetails(contact);
// Copy constructor
memberID = other.memberID;
setName(other.name);
setContactDetails(other.contactDetails);
// Destructor
~LibraryMember() {
delete[] name;
delete[] contactDetails;
if (n == nullptr) return;
strcpy(name, n);
strcpy(contactDetails, contact);
};
int main() {
book1.displayDetails();
member1.displayDetails();
return 0;
OUTPUT:
Question 5:
Write a C++ program to design a class 'Complex' with data members for real and
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
// Default constructor
Complex result;
return result;
Complex result;
return result;
Complex result;
return result;
}
// Division operator overloading
Complex result;
return result;
if (imag < 0)
std::cout << real << " - " << -imag << "i" << std::endl;
else
std::cout << real << " + " << imag << "i" << std::endl;
};
int main() {
// Displaying results
sum.display();
diff.display();
prod.display();
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
#include <iostream>
#include <string>
class StateInformation {
private:
std::string stateName;
double area;
double populationDensity;
public:
// Default constructor
stateName = name;
}
// Setter for area
void setArea(double a) {
area = a;
populationDensity = pd;
std::cout << "Area: " << area << " square kilometers" << std::endl;
std::cout << "Population Density: " << populationDensity << " people per square kilometer" <<
std::endl;
};
int main() {
state1.displayInformation();
state3.displayInformation();
return 0;
return 0;
}Output:
Question 7:
#include <iostream>
class DynamicArray {
private:
int* data;
int size;
public:
DynamicArray(int s) : size(s) {
~DynamicArray() {
delete[] data;
data[index] = value;
else
return data[index];
else {
};
int main() {
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);
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.