Professional Documents
Culture Documents
Name:
Class:
Group:
No.:
Session: Mon
LABORATORY ASSIGNMENT 4
Report
Lab Objectives
• Discovering classes
• Encapsulation
• Using the concepts of inheritance and polymorphism
Lab check
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;
if ( avg_profit> best_profit)
{ best_profit = avg_profit;
best_city = city;
}
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>
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 = "";
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();
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
5|Page
Laboratory course Programming language C++
string zipcode;
}
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);
6|Page
Laboratory course Programming language C++
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
Card::Card(string n)
{ name = n;
}
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;
};
private:
int ID_number;
};
private:
int Card_Number;
int PIN;
};
int main()
{
}
Card::Card()
{ name = "";
}
8|Page
Laboratory course Programming language C++
Card::Card(string n)
{ name = n;
}
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.
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.
10 | P a g e
Laboratory course Programming language C++
Have a main function populate a Billfold object with Card* objects, and then
call print_cards.
Your answer
Your answer
11 | P a g e