This is the Aggregation Program. it has three .h files and one .cpp file. I copi
ID: 639753 • Letter: T
Question
This is the Aggregation Program. it has three .h files and one .cpp file. I copied from the book in order.. i need to undertand how it works. can someone explain me how the Agregation thing works. can you coment on each line, what happens? what it does?i need detailed explanation of the program. thanks.
//first .h file.
#ifndef INSTRUCTOR
#define INSTRUCTOR
#include <iostream>
#include <string>
using namespace std;
// Instructor class
class Instructor
{
private:
string lastName; // Last name
string firstName; // First name
string officeNumber; // Office number
public:
// The default constructor stores empty strings
// in the string objects.
Instructor()
{ set("", "", ""); }
// Constructor
Instructor(string lname, string fname, string office)
{ set(lname, fname, office); }
// set function
void set(string lname, string fname, string office)
{ lastName = lname;
firstName = fname;
officeNumber = office; }
// print function
void print() const
{ cout << "Last name: " << lastName << endl;
cout << "First name: " << firstName << endl;
cout << "Office number: " << officeNumber << endl; }
};
#endif
//second .h file.
#ifndef TEXTBOOK
#define TEXTBOOK
#include <iostream>
#include <string>
using namespace std;
// TextBook class
class TextBook
{
private:
string title; // Book title
string author; // Author name
string publisher; // Publisher name
public:
// The default constructor stores empty strings
// in the string objects.
TextBook()
{ set("", "", ""); }
// Constructor
TextBook(string textTitle, string auth, string pub)
{ set(textTitle, auth, pub); }
// set function
void set(string textTitle, string auth, string pub)
{ title = textTitle;
author = auth;
publisher = pub; }
// print function
void print() const
{ cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Publisher: " << publisher << endl; }
};
#endif
//third .h file.
#ifndef COURSE
#define COURSE
#include <iostream>
#include <string>
#include "Instructor.h"
#include "TextBook.h"
using namespace std;
class Course
{
private:
string courseName; // Course name
Instructor instructor; // Instructor
TextBook textbook; // Textbook
public:
// Constructor
Course(string course, string instrLastName,
string instrFirstName, string instrOffice,
string textTitle, string author,
string publisher)
{ // Assign the course name.
courseName = course;
// Assign the instructor.
instructor.set(instrLastName, instrFirstName, instrOffice);
// Assign the textbook.
textbook.set(textTitle, author, publisher); }
// print function
void print() const
{ cout << "Course name: " << courseName << endl << endl;
cout << "Instructor Information: ";
instructor.print();
cout << " Textbook Information: ";
textbook.print();
cout << endl; }
};
#endif
//and last one .cpp file.
// This program demonstrates the Course class.
#include "Course.h"
int main()
{
// Create a Course object.
Course myCourse("Intro to Computer Science", // Course name
"Kramer", "Shawn", "RH3010", // Instructor info
"Starting Out with C++", "Gaddis", // Textbook title and author
"Addison-Wesley"); // Textbook publisher
// Display the course info.
myCourse.print();
return 0;
}
Explanation / Answer
> Encapsulation in Object Oriented Programming has following features:
1) Data Hiding
2) Binding up of data and functions in a single unit
2) It doesnot allow the data or the code to be accessed outside the class
-> The main benefit of Encapsulation is maintainability,flexibiltity and extensibility
-> The Main Program displays the Details of Course,Details of Instructor,Details Of TextBook
-> In the example:
Class Variables Constructor Function AnotherClass Instance
Instructor lastName,firstName,officeNumber - set,print
TextBook author,publisher - set,print
Course courseName,instructor,textbook Course print Instructor,TextBook
* Aggregation is a specific type of composition where no ownership between the complex object and the subobjects is implied. When an aggregate is destroyed, the subobjects are not destroyed.
* It occurs when a class contains an instance of another class
Thus Course is an aggegration of Instructor amd TextBook Class
-> With the explanation of the program
$ A good programming principle is to separate the related defination of the items in the own class
$ Two independent class are created
# first.h defines the class Instructor which will give details about the lastName,firstName and officeNumber of the Instructor with the set() to set the values and print() to print the values
# second.h defines the class TextBook which will give the details about the title,author,publisher of the TextBook the Instructor is using with the set() to set the values and print() to print the values
# third.h has following explanation which aggregrates Instructor and TextBook class
#include <iostream>
#include <string>
#include "Instructor.h"
#include "TextBook.h"
using namespace std;
class Course
{
private:
string courseName; // Course name
Instructor instructor; // Instructor -> Instructor Class object created as Member Variable which are defined outside the scope of the Class
TextBook textbook; // Textbook-> TextBook Class object created as Member Variable which are defined outside the scope of the Class
// Consequently, an aggregate class usually either takes the objects it is going to point to as constructor parameters, or it begins empty and the subobjects are added later via access functions or operators.Because these subclass objects live outside of the scope of the class, when the class is destroyed, the pointer or reference member variable will be destroyed, but the subclass objects themselves will still exist.
public:
// Constructor
Course(string course, string instrLastName,
string instrFirstName, string instrOffice,
string textTitle, string author,
string publisher)
{ // Assign the course name.
courseName = course;
// Assign the instructor.
instructor.set(instrLastName, instrFirstName, instrOffice);
// Assign the textbook.
textbook.set(textTitle, author, publisher); }
// print function
void print() const
{ cout << "Course name: " << courseName << endl << endl;
cout << "Instructor Information: ";
instructor.print();
cout << " Textbook Information: ";
textbook.print();
cout << endl; }
};
#include "Course.h"
int main()
{
// Create a Course object.
Course myCourse("Intro to Computer Science", // Course name
"Kramer", "Shawn", "RH3010", // Instructor info
"Starting Out with C++", "Gaddis", // Textbook title and author
"Addison-Wesley"); // Textbook publisher
-> This will call the Constructor of the program Course class Course(string course, string instrLastName,string instrFirstName, string instrOffice,string textTitle, string author,string publisher)
and assign the values as specified to the parameters as :
{ // Assign the course name.
courseName = "Intro To Computer Science";
// Assign the instructor.
instructor.set("Kramer", "Shawn", "RH3010");// Using the set function of the Instructor Class
// Assign the textbook.
textbook.set( "Starting Out with C++", "Gaddis", "Addison-Wesley"); //Using the set function of the TextBook Class
// Display the course info.
myCourse.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.