You are on page 1of 11

Laboratory course Programming language C++

Name:
Class:
Group:
No.:
Session: Mon

LABORATORY ASSIGNMENT 4
Report

Lab Objectives

To gain experience with

• Discovering classes
• Encapsulation
• Using the concepts of inheritance and polymorphism

Lab check

Please check your status of assignment:


Title Content Can’t do it Finished
Pre-lab assignments Discovering classes
In-lab assignments  Encapsula
tion
Inheritance

Polymorphism

1|Page
Laboratory course Programming language C++

Pre-Lab.

Discovering classes

In each of the following examples, locate and describe those conceptually related
procedures and data elements that can be combined into a single class. Rewrite the
program using class.
/* NAME : best_profit.cpp
PURPOSE : Calculates the best city for making a profit as a housing
contractor.
*/

#include <iostream>
#include <string>
using namespace std;

int main()
{
bool mdata = true;
string response = "";
double avg_sale_price = 0.0;
double avg_building_cost = 0.0;
double avg_profit = 0.0;
double best_profit = 0.0;
string city = "";
string best_city = "";

while (mdata)
{ cout << "Average building cost: ";
cin >> avg_building_cost;

cout << "Average sale price: ";


cin >> avg_sale_price;

cout << "City : ";


cin >> city;
avg_profit = avg_sale_price - avg_building_cost;

if ( avg_profit> best_profit)
{ best_profit = avg_profit;
best_city = city;
}

cout << "More data (y/n)? ";


cin >> response;
if (response != "y")
{ mdata = false;
}
}

cout << "The best city to be a builder in is: "<<city << "\n";

2|Page
Laboratory course Programming language C++

return 0;
}

/* NAME : best_profit.cpp
PURPOSE : Calculates the best city for making a profit as
a housing
contractor.
*/

#include<iostream>
#include<string>

using namespace std;

class City
{
public:
City(); //defaut constructor

void set_avg_sale_price();
void set_avg_building_cost();
void set_city();

void test();
string get_best_city();

private:
double avg_sale_price;
double avg_building_cost;
double avg_profit;
double best_profit;

string city;
string best_city;
};

int main()
{
bool mdata = true;
string response = "";

cout <<"Begin the program: \n\n";

City The_city;

3|Page
Laboratory course Programming language C++

while(mdata)
{
The_city.set_avg_building_cost();
The_city.set_avg_sale_price();
The_city.set_city();
The_city.test();

cout << "More data (y/n)? ";


cin >> response;
if (response != "y")
{
mdata = false;
}
}
cout << "The best city to be a builder in is:
"<<The_city.get_best_city()<< "\n";
return 0;
}

City::City()
{
double avg_sale_price = 0.0;
double avg_building_cost = 0.0;
double avg_profit = 0.0;
double best_profit = 0.0;
string city = "";
string best_city = "";
}

void City::set_city()
{
cout<<"Name of the city is:(Don't use space) ";
cin>>city;
}

void City::set_avg_building_cost()
{
cout << "Average building cost: ";
cin >> avg_building_cost;
}

void City::set_avg_sale_price()

4|Page
Laboratory course Programming language C++

{
cout << "Average sale price: ";
cin >> avg_sale_price;
}

void City::test()
{
avg_profit=avg_sale_price-avg_building_cost;

if(avg_profit>best_profit)
{
best_profit = avg_profit;
best_city = city;
}
}

string City::get_best_city()
{
return best_city;
}

In-Lab 1.

Encapsulation

Consider the following class declaration.


class Customer
{
public:
Customer(string name, string address, string city, string state, string
zipcode);

void increase_limit(double amount);


string get_name() const;
string get_address() const;
string get_city() const;
string get_state() const;
double credit_limit;
private:
string name;
string address;
string city;
string state;

5|Page
Laboratory course Programming language C++

string zipcode;
}

class Customer fails to effectively encapsulate a data member, thereby risking a


runtime error due to corrupted data. Locate the problem(s) and propose a solution.
class Customer
{
public:
Customer(string name, string address, string city, string state, string
zipcode);

void increase_limit(double amount);


string get_name() const;
string get_address() const;
string get_city() const;
string get_state() const;
private:
string name;
string address;
string city;
string state;
string zipcode;
double credit_limit;
}
Vì credit_limit là dữ liệu thành phần nên cần phải đưa vào private
để đóng gói hiệu quả

One of the data members is properly encapsulated but its value cannot be detected.
What is it, and what member function might be added later that might use it?
class Customer
{
public:
Customer(string name, string address, string city, string state, string
zipcode);

void increase_limit(double amount);


string get_name() const;
string get_address() const;
string get_city() const;
string get_state() const;
string get_zipcode() const;
private:
string name;
string address;
string city;
string state;
string zipcode;
double credit_limit;
}

6|Page
Laboratory course Programming language C++

Cần có thêm hàm string get_zipcode() const; để lấy dữ liệu của


zipcode

In-Lab 2.

Inheritance

Consider using the following Card class as a base class to implement a hierarchy of
related classes: (assume that DayOfYear is pre-defined)

Class Data

IDcard ID number (int)

Calling Card Card number (int), PIN (int)

DriverLicense Expiration date (int)


class Card
{
public:
Card();
Card(string n);
virtual void print() const;
private:
string name;
};
Card::Card()
{ name = "";
}

Card::Card(string n)
{ name = n;
}

void Card::print() const


{

Write definitions for each of the derived classes. For each derived class, supply
private data members and the declarations (but not the definitions) of the constructors
(not default constructor) and the print function:

#include<iostream>

7|Page
Laboratory course Programming language C++

#include<string>
using namespace std;

class Card
{
public:
Card();
Card(string n);
virtual void print() const;
private:
string name;
};

class IDcard : public Card


{
public:
IDcard();
IDcard(string n, int The_ID_number);
void print() const;

private:
int ID_number;
};

class Calling_Card : public Card


{
public:
Calling_Card();
Calling_Card(string n, int The_Card_Number,int The_PIN);
void print() const;

private:
int Card_Number;
int PIN;
};
int main()
{

}
Card::Card()
{ name = "";
}

8|Page
Laboratory course Programming language C++

Card::Card(string n)
{ name = n;
}

void Card::print() const


{
//return name;
}

IDcard::IDcard()
{
ID_number=0;
}
IDcard::IDcard(string n,int The_ID_number)
{
ID_number=The_ID_number;
}
void IDcard::print() const
{
cout<<ID_number;
}

Calling_Card::Calling_Card()
{
Card_Number=0;
PIN=0;
}
Calling_Card::Calling_Card(string n,int The_Card_Number,int The_PIN)
{
Card_Number=The_Card_Number;
PIN=The_PIN;
}
void Calling_Card::print() const
{
cout<<Card_Number<<" "<<PIN;
}

Implement constructors for each of the three derived classes. Each constructor
needs to call the base class constructor to set the name.

IDcard::IDcard()

9|Page
Laboratory course Programming language C++

{
ID_number=0;
}
IDcard::IDcard(string n,int The_ID_number)
{
ID_number=The_ID_number;
}
void IDcard::print() const
{
cout<<ID_number;
}

Calling_Card::Calling_Card()
{
Card_Number=0;
PIN=0;
}
Calling_Card::Calling_Card(string n,int The_Card_Number,int The_PIN)
{
Card_Number=The_Card_Number;
PIN=The_PIN;
}
void Calling_Card::print() const
{
cout<<Card_Number<<" "<<PIN;
}

In-Lab 3.

Polymorphism and Virtual function

Supply the implementation of a virtual print function for the card class and
corresponding overloaded functions in each of the other three derived classes. The
derived class functions need to call the base class print to print the name of the
cardholder.

Your code here

10 | P a g e
Laboratory course Programming language C++

Devise another class, Billfold, which contains a vector<Card*>, a member


function add_card(Card*) and a member function print_cards(). Of
course, print_cards invokes the virtual print function on each card.

Your code here

Have a main function populate a Billfold object with Card* objects, and then
call print_cards.

Your code here

Show the output of your test run.

Your answer

Is print_cards a virtual function? Explain.

Your answer

11 | P a g e

You might also like