Docsity

Object Oriented Programming, Assignments of Object Oriented Programming

Toggle sidebar
bg1
Name :
Roll no :
Register Number :
Date of submission :
Ex. No. 3: Classes and Objects
(Constructors & Destructors)
Objective
To understand and implement the concepts of classes, objects, constructors, destructors,
constructor overloading, and constant member functions in C++ classes for object
initialization, cleanup, and structured data handling by defining user-defined classes with data
members and member functions to model real-world entities
Program 1:
Create a class named Book with attributes such as title, author, and ISBN. Define
appropriate member functions to get book information from the user and display the
book details.
Algorithm
1. Start the program.
2. Define a Book class with data members: title, author, and ISBN.
3. Define member functions to read book details and display them.
4. Create an object of the Book class in main().
5. Call the member functions using the object.
6. Display the book details.
7. Stop the program.
Program:
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title;
string author;
string ISBN;
public:
// Function to get book information from the user
void getBookInfo() {
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter author name: ";
getline(cin, author);
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Object Oriented Programming and more Assignments Object Oriented Programming in PDF only on Docsity!

Name :

Roll no :

Register Number :

Date of submission :

Ex. No. 3: Classes and Objects

(Constructors & Destructors)

Objective To understand and implement the concepts of classes, objects, constructors, destructors, constructor overloading, and constant member functions in C++ classes for object initialization, cleanup, and structured data handling by defining user-defined classes with data members and member functions to model real-world entities Program 1: Create a class named Book with attributes such as title, author, and ISBN. Define appropriate member functions to get book information from the user and display the book details. Algorithm

1. Start the program.

  1. Define a Book class with data members: title, author, and ISBN.
  2. Define member functions to read book details and display them.
  3. Create an object of the Book class in main().
  4. Call the member functions using the object.
  5. Display the book details.
  6. Stop the program. Program: #include #include using namespace std; class Book { private: string title; string author; string ISBN; public: // Function to get book information from the user void getBookInfo() { cout << "Enter book title: "; getline(cin, title); cout << "Enter author name: "; getline(cin, author);

cout << "Enter ISBN: "; getline(cin, ISBN); } // Function to display book details void displayBookInfo() { cout << "\nBook Details:\n"; cout << "Title : " << title << endl; cout << "Author : " << author << endl; cout << "ISBN : " << ISBN << endl; } }; int main() { Book b; b.getBookInfo(); b.displayBookInfo(); return 0; } Output: Program 2: Create a class named CarPark with data members Car Registration Number, Charge Per Hour, and Parking Duration. Implement appropriate member functions to set the data and display the parked hours and total charges. Algorithm

  1. Start the program.
  2. Define the CarPark class with required data members.
  3. Define member functions to set and display parking details.

Output:

Program 3: Create a class named BankAccount with data members accountNumber,accountHolderName, and balance.Implement member functions to deposit an amount, withdraw an amount, and display the current balance. Implement constructor overloading by defining: โ— A parameterized constructor that initializes the account with account number, account holder name. This Constructor should initialize the account balance as 0. โ— A parameterized constructor that initializes the account with account number, account holder name, and initial balance Create objects of the class using different constructors and perform deposit and withdrawal operations. Algorithm:

  1. Start
  2. Create BankAccount class with account number, name, balance
  3. Initialize balance using constructors
  4. Add deposit() to increase balance
  5. Add withdraw() to decrease balance if sufficient
  6. Add displayBalance() to show details
  7. Create objects in main and call functions
  8. Stop Program: #include #include using namespace std; class BankAccount { private: int accountNumber; string accountHolderName; double balance; public: //Constructor 1 BankAccount(int accNo, string name) { accountNumber = accNo; accountHolderName = name; balance = 0.0; } // Constructor 2 BankAccount(int accNo, string name, double initialBalance) {

Program 4: Develop a Program to demonstrate mathematical computation using classes and interaction between multiple objects. Create a class to represent a point in a two- dimensional coordinate system with data members for the x-coordinate and y- coordinate. Implement member functions to accept coordinate values from the user and to calculate the distance between two-point objects. Further, enhance the program by using constructors to initialize point coordinates. Apply constructor overloading by defining a default constructor that initializes a point at the origin and a parameterized constructor that initializes a point with given coordinates. Create appropriate objects and display the calculated distance between them. Algorithm: Program: Output:

cout<<"Product Id:"; cin>>ID; cout<<"Name of the product:"; cin>>name; cout<<"Price of the product:"; cin>>price; cout<<"Number of products:"; cin>>number; } void calculatecost(){ totalcost = price*number; } void display(){ cout<<"Details of the product:"<

  1. Write a program to create a MyString class containing a character array and a variable to store the length of the string. Implement a default constructor to initialize the string as empty and a parameterized constructor to initialize it with a user-specified string. Define a member function to compute the length of the string and display the string and its length. Program: Output:
  1. Write a program to calculate the sum of two distances given in feet and inches. Design a Distance class with a parameterized constructor to initialize the values. Implement a member function to add two Distance objects and display the result, ensuring inches are properly converted into feet when they exceed 12. Program: Output: