You are on page 1of 12

LAB#3 ROLL NO:DT-45

/*Write a C++ program to copy the value of one object to another object using
copy constructor.*/

#include <iostream>

using namespace std;

class Number {

private:

int a ;

public:

Number(){}

Number(int x){

a = x;

cout<<"constructor Called"<<endl;

Number(Number &obj){

a = obj.a;

cout<<"Copy constructor called"<<endl;

void Display(){

cout<<"The values are: "<< a <<endl;

};

int main() {

Number num1(10);
LAB#3 ROLL NO:DT-45

Number num2 = num1;

num1.Display();

num2.Display();

return 0;

2. Create a class tollbooth. The two data items are a type int to hold the total number

of cars and a type double to hold the total amount of money collected. A

constructor initializes both these to 0. When a car passes the toll, a member

function called payingCar( ) increments the car total and adds 0.50 to the cash

total. Another member function displays the two totals. DESIGN and IMPLEMENT

this case. Make assumptions (if required) and include it in the description before

designing the solution.*/

#include <iostream>

using namespace std;

class tollbooth

public:

int totalcars;

double totalMoney;

tollbooth()

{
LAB#3 ROLL NO:DT-45

totalcars = 0;

totalMoney = 0;

cout << "Constructor Created" << endl;

void paying_car()

totalcars++;

totalMoney += 0.50;

void displayTotals() const

cout << "Number of cars passed: " << totalcars << endl;

cout << "Total Money: " << totalMoney << endl;

};

int main()

tollbooth cars;

cars.paying_car();

cars.paying_car();

cars.paying_car();

cars.paying_car();

cars.displayTotals();

return 0;
LAB#3 ROLL NO:DT-45

/*Some of the characteristics of a book are the title, author(s), publisher, ISBN,
price,

and year of publication. Design a class bookType that defines the book as an
ADT.

Each object of the class bookType can hold the following information about a

book: title, up to four authors, publisher, ISBN, price, and number of copies in

stock. To keep track of the number of authors, add another member variable.

Include the member functions to perform the various operations on objects of

type bookType.

For example, the usual operations that can be performed on the title are to

show the title, set the title, and check whether a title is the same as the actual

title of the book. Similarly, the typical operations that can be performed on the

number of copies in stock are to show the number of copies in stock, set the

number of copies in stock, update the number of copies in stock, and return the

number of copies in stock. Add similar operations for the publisher, ISBN, book

price, and authors. Add the appropriate constructors and a destructor (if one is

needed).

Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and tests various operations on

the objects of the class bookType. Declare an array of 100 components of type

bookType. Some of the operations that you should perform are to search for a

book by its title, search by ISBN, and update the number of copies of a book.*/

#include <iostream>

#include <string>

using namespace std;


LAB#3 ROLL NO:DT-45

class booktype

public:

string actualTitle;

string title2;

string publisher;

string ISBN;

double price;

int numofcopies;

int numofauthors;

string authors[4];

booktype()

actualTitle = "";

title2 = "";

publisher = "";

ISBN = "";

price = 0.0;

numofcopies = 0;

numofauthors = 0;

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

authors[i] = "";

}
LAB#3 ROLL NO:DT-45

void set_title()

cout << "Enter a Title for the book: " << endl;

cin >> title2;

void display_title()

cout << "The title set for the book is: " << title2 << endl;

void checkTitle()

if (title2 == actualTitle)

cout << "The Actual title and the set title are the same" << endl;

else

cout << "The Actual Title and Set title are different" << endl;

}
LAB#3 ROLL NO:DT-45

void set_copies()

cout << "Enter the number of copies: ";

cin >> numofcopies;

void display_copies()

cout << "The number of copies is: " << numofcopies << endl;

void set_publisher()

cout << "Enter the name of the publisher: ";

cin >> publisher;

void display_publisher()

cout << "The name of the publisher is: " << publisher << endl;

void set_ISBN()

cout << "Enter the ISBN number of the book: "<<endl;


LAB#3 ROLL NO:DT-45

cin>> ISBN;

void display_ISBN()

cout << "ISBN number of the book is: " << ISBN << endl;

void set_authors()

cout << "Enter the number of authors: ";

cin >> numofauthors;

if (numofauthors <= 4)

for (int i = 0; i < numofauthors; i++)

cout << "Enter the name of author " << i + 1 << ": ";

cin >> authors[i];

void display_authors()
LAB#3 ROLL NO:DT-45

cout << "The Name/s of the author/s are: " << endl;

for (int i = 0; i < numofauthors; i++)

cout << authors[i] << endl;

void set_price()

cout << "Enter the price of the book: ";

cin >> price;

void display_price()

cout << "The Price of the book is: " << price << endl;

bool searchByTitle(const string& searchTitle) {

return title2 == searchTitle;

bool searchByISBN(const string& searchISBN) {

return ISBN == searchISBN;


LAB#3 ROLL NO:DT-45

void updateCopies(int newCopies) {

numofcopies = newCopies;

};

int main()

booktype books[100];

books[0].set_title();

books[0].display_title();

books[0].set_authors();

books[0].display_authors();

books[0].set_copies();

books[0].

display_copies();

books[0].set_publisher();

books[0].display_publisher();

books[0].set_ISBN();
LAB#3 ROLL NO:DT-45

books[0].display_ISBN();

books[0].set_price();

books[0].display_price();

// Search by title

string searchTitle;

cout << "Enter a title to search: ";

cin >> searchTitle;

if (books[0].searchByTitle(searchTitle))

cout << "Book found!" << endl;

else

cout << "Book not found!" << endl;

// Search by ISBN

string searchISBN;

cout << "Enter an ISBN to search: ";

cin >> searchISBN;

if (books[0].searchByISBN(searchISBN))
LAB#3 ROLL NO:DT-45

cout << "Book found!" << endl;

else

cout << "Book not found!" << endl;

int newCopies;

cout << "Enter the new number of copies: ";

cin >> newCopies;

books[0].updateCopies(newCopies);

books[0].display_copies();

return 0;

You might also like