Question
Instruction: Answer all questions. Write your answer to the table located on the final page of this document. Do not remove any pages. Activity 1:
Instruction: Answer all questions. Write your answer to the table located on the final page of this document. Do not remove any pages.
Activity 1: Base Class
Given is a C++ program that consists of a class name Book. The value is pre-defined by the programmer.
#include using namespace std;
class Book{ public: int id; string title; string author; int year; float price; };
int main() { Book book1;
book1.id = 1; book1.title = "Learn C++ Programming"; book1.author = "Chand Miyan"; book1.year = 2015; book1.price = 99.9;
cout << "Book ID: " << book1.id << endl << "Book Title: " << book1.title << endl << "Book Author: " << book1.author << endl << "Year Published: " << book1.year << endl << "Book Price: RM" << book1.price << endl; return 0; }
|
Run the program and observe the output.
Add one more Book object name book2. Set the value of class members to the following information.
Book ID: 2
Book Name: Computer Organization and Architecture
Book Author: William Stallings
Book Price: RM79.00
Publication year: 2019
The program should display the information on both objects, book1 and book2.
#include using namespace std;
class Book{ public: int id; string title; string author; int year; float price; };
int main() { Book book1, book2; book1.id = 1; book1.title = "Learn C++ Programming"; book1.author = "Chand Miyan"; book1.year = 2015; book1.price = 99.9;
book2.id = 2; book2.title = "Computer Organization and Architecture"; book2.author = "William Stallings"; book2.year = 2019; book2.price = 79.9;
cout << "Book ID: " << book1.id << endl << "Book Title: " << book1.title << endl << "Book Author: " << book1.author << endl << "Year Published: " << book1.year << endl << "Book Price: RM" << book1.price << endl;
cout << "Book ID: " << book2.id << endl << "Book Title: " << book2.title << endl << "Book Author: " << book2.author << endl << "Year Published: " << book2.year << endl << "Book Price: RM" << book2.price << endl; return 0; }
|
Add one more Book object name book3. Set the value of class members to the following information.
Book ID: 3
Book Name: Professional CUDA C Programming
Book Author: John Cheng
Year Published: 2018
Book Price: RM180.00
The program should display the information on the three objects created, book1, book2, and book3.
Run your program and screen capture your code and the output.
Activity 2: Private and public access control
Given is a C++ program that consists of a class name Measurement. The program will ask the user to key in the measurements information and display the values entered by the user.
#include using namespace std;
class Measurement{ private: int width; int length; int height;
public: void setWidth(int w){ width = w; } void setLength(int l){ length = l; } void setHeight(int h){ height = h; } int getWidth(void){ return width; } int getLength(void){ return length; } int getHeight(void){ return height; } int calculateArea(){ return width*length; } };
int main() { Measurement shape1; int width,length,height; cout << "Set the values for width, length, and height "; cin >> width; cin >> length; cin >> height; shape1.setWidth(width); shape1.setLength(length); shape1.setHeight(height);
cout << "Values entered by the user are: " << "Width: " << shape1.getWidth() << endl << "Length: " << shape1.getLength() << endl << "Height: " << shape1.getHeight() << endl; cout << "Area: " << shape1.calculateArea() << endl;
return 0; } |
Continue from the program given in Activity 2, add one more public function name calculateVolume into the Measurement class. This function will return the calculation results of width*length*height. The main function will display the volume result.
Run your program and screen capture your code and the output.
Activity 3: Inheritance
We are using the same example as in Activity 2. We will enable the protected access for the class members, and we will be using public member functions to access the class members. We will have another class, derive class that inherits the Measurement class.
#include using namespace std;
class Measurement{ protected: int width; int length; int height;
public: void setWidth(int w){ width = w; } void setLength(int l){ length = l; } void setHeight(int h){ height = h; } int getWidth(void){ return width; } int getLength(void){ return length; } int getHeight(void){ return height; } };
class Calculate: public Measurement{ public: int calculateArea(){ return width*length; } };
int main() { Calculate shape1; int width,length,height; cout << "Set the values for width, length, and height "; cin >> width; cin >> length; cin >> height; shape1.setWidth(width); shape1.setLength(length); shape1.setHeight(height);
cout << "Values entered by the user are: " << "Width: " << shape1.getWidth() << endl << "Length: " << shape1.getLength() << endl << "Height: " << shape1.getHeight() << endl; cout << "Area: " << shape1.calculateArea() << endl;
return 0; } |
Continue from the program given in Activity 3, add one more public function name calculateVolume into the derive class, Calculate. This function will return the calculation results of width*length*height. The main function will display the volume result.
Run your program and screen capture your code and the output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started