Question

In: Computer Science

Step1 - Define two classes, Whole and part. Use a strong "has a" relationship with the...

Step1 - Define two classes, Whole and part. Use a strong "has a" relationship with the two classes.

            Constructor Functions

            Define a default constructor for the Part class.

                        Default just prints out "In default part constructor"

            Define a default and a parm constructor for the Whole class.

                        In Whole Default constructor assigns wholeName = "None"; and prints out "In whole default Constructor"

                        In Whole Parm constructor - Passes in name to WholeName and

                        Then It prints out value of wholeName variable and "In Whole parm constructor"

            Destructor Functions

            Define a destructor function for the part class.

            Define a string variable called wholeName in the whole class.

            Define a getName function for whole class.

Two test.

Step 2 - Declare a Whole W1, with default constructor

             Have the Whole constructor print "called default constructor", and contents of wholeName variable

             Have the Whole Destructor print "called destructor", and contents of wholeName variable

            Declare a Whole W2, with a parm constructor - Have it pass in "w2"

             Have the Whole constructor print "called parm constructor", and contents of wholeName variable.

             Have the Whole Destructor print "called destructor", and the contents of wholeName variable.

Step 3 - use the dot

            Use the dot notation to print out the contents of W1 wholeName variable

            Use the dot notation to print the contents of W2 wholeName variable

Solutions

Expert Solution

HAS-A Relationship:

Basically the term Composition reffers to the HAS-A Relationship i.e the use of instance variables that are references to the other objects.

In our code snippet we have declared HAS-A Relationship as : class Part: public Whole

Find below code for the above scenario:

Code;

#include <iostream>

using namespace std;
class Whole{ // Whole Class defination
public:
string wholeName;
void getName(){ //getName function for whole class.
  
}
Whole(){ //Default constructor of a Whole class
string wholeName="None"; //Default constructor Assignmnt to the wholeName variable
cout<<"In whole default Constructor\n";
  
}
Whole(string wholeName){ //Parm constructor of a Whole class
cout<<wholeName;
cout<<"In Whole parm constructor\n";
  
}
~Whole(){ //Destructor function
cout<<"called destructor\n";
cout<<wholeName;
}
  
  
};
class Part: public Whole{ //Has-A Relationship implementation
public:
Part(){ //Default constructor of a Part class
cout<< "In default part constructor";
}
~Part(){ //Destructor function
  
}
  
};
int main()
{
Whole W1("called default constructor\n");
cout<<W1.wholeName;   // dot notation to print out the contents of W1
Whole W2("Called parm constructor\n");
cout<<W2.wholeName; // dot notation to print out the contents of W2
return 0;
}

Code Snapshots with output:

I hope you find this helpful if not please comment below i will help you.

Thanks, Do not forget to upvote the answer!!

Happy Learning!! :)


Related Solutions

7. When a class contains objects of another class, the relationship is called a whole-part relationship...
7. When a class contains objects of another class, the relationship is called a whole-part relationship or composition. The relationship created is also called a has-a relationship. Discuss some of the possible issues with composition, such CIS216 – Programming Principles Object-Oriented Programming as the long statement: output sales.getHighestPaidEmployee().getHireDate().getYear(). 8. Describe the rules that govern the attributes of a parent class that can be accessed by a child class and the reverse 9. Discuss the advantages of using inheritance and why...
Define politics and government. What is the relationship between the two?
Define politics and government. What is the relationship between the two?
The relationship between sleep and performance is known to be strong (positive linear relationship). The relationship...
The relationship between sleep and performance is known to be strong (positive linear relationship). The relationship between junk food and performance is a strong, but inverse. Given this information, what conclusions can you draw about the relationship between sleep and junk food?
Explain an Is-a relationship between classes in Java
Explain an Is-a relationship between classes in Java
Use the following to answer questions 6 – 12 TR, Inc., has two classes of stock...
Use the following to answer questions 6 – 12 TR, Inc., has two classes of stock authorized: $100.00 par preferred and $0.01 par value common. As of the beginning of 20XD, 10,000 shares of preferred stock and 600,000 shares of common stock have been issued. Record the following transactions to complete the Statement of Stockholders’ Equity: Affect on Stockholders’ Equity 1-Mar Issue 100 additional shares of preferred stock for $103.50 per share 1-Apr Issue 100,000 additional shares of common stock...
Case Study: A Tale of Two Classes Ebony Ellis has two communication classes back-to-back in the...
Case Study: A Tale of Two Classes Ebony Ellis has two communication classes back-to-back in the same room, but they couldn’t be more different. The first, a class on interpersonal communication, is taught by Steve Gardner, an older professor who has taught at the university for 20 years. The first day of class he verbally explained the rules for class conduct, which were also distributed in a printed handout—cell phones off, no texting, and, unless a student needs to use...
Think of a pair of variables that has a strong linear relationship, either positive or negative,...
Think of a pair of variables that has a strong linear relationship, either positive or negative, then find data that you think supports your assertion. You need to get real data for this problem, not just make numbers up to fit your hypothesis.
The nonprofit sector has a history of being strong at the diversity and inclusion part of...
The nonprofit sector has a history of being strong at the diversity and inclusion part of the Diversity, Equity, Inclusion call to action; however, equity is still something that we are struggling with currently. How do organizations improve their language use, and why it could be an important shift in today's society?
A small school has only two 4th grade classes and two 5th grade classes. Each year,...
A small school has only two 4th grade classes and two 5th grade classes. Each year, students at the school take the Nebraska Test of Basic Skills (NTBS) in math and Language Arts. The Excel file, 745 Project Data, contains all of the scores for a certain class tracked over their 4th and 5th grade years. Create a frequency distribution and cumulative frequency distribution of the 4th Grade Math test scores for all of the students. Use 5 classes. Create...
A small school has only two 4th grade classes and two 5th grade classes. Each year,...
A small school has only two 4th grade classes and two 5th grade classes. Each year, students at the school take the Nebraska Test of Basic Skills (NTBS) in math and Language Arts. The Excel file, 745 Project Data, contains all of the scores for a certain class tracked over their 4th and 5th grade years. Create a frequency distribution and cumulative frequency distribution of the 4th Grade Math test scores for all of the students. Use 5 classes. Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT