WHAT I HAVE SO FAR<<< #include #include using namespace std; class Beta //associ
ID: 3817809 • Letter: W
Question
WHAT I HAVE SO FAR<<<
#include
#include
using namespace std;
class Beta //association to Alpha
{
private:
double cost;
public:
Beta(char a_cost) : cost(a_cost) {}
void display() { cout << cost << endl; }
};
class Delta //association to Alpha
{
private:
char part;
string desc;
public:
Delta(char a_part, int a_id, string a_name, string desc);
void display() { cout << desc << endl; }
};
class Gamma //composition to Alpha
{
private:
int id;
string name;
public:
Gamma(int a_id, string name) : id(a_id) {}
void display() { cout << id << " " << name << endl; }
};
class Alpha //must be after Gamma, Beta and Delta
{
private:
char part;
Gamma gamma; // composition
Beta* beta; // aggregation
Delta* delta; //aggregation
public:
Alpha(char a_part, int a_id, string a_name) : part(a_part), gamma(a_id, a_name), delta(a_part, a_id, a_name, desc), beta(NULL) {}
void setBeta(double a_cost)
{
if (beta != NULL)
delete beta;
beta = new Beta(a_cost);
}
void display()
{
cout << part << endl;
gamma.display();
beta->display();
delta->display();
}
};
int main()
{
//Implement a main that instantiates a Delta,
//object, sets a Beta, and then displays the Delta
Delta delta1('j', 698, "MIKE", "JONES");
delta1.setBeta(4.5);
delta1.display();
return 0;
}
Explanation / Answer
Given below is a C++ implementation of four classes (namely - Alpha, Beta, Gamma and Delta) mentioned in the given UML diagram.
The relationships between these four classes are as follows.
Before accessing Beta (in setBeta, destructor, display) from within Alpha confirm it is not null.
File:Greek.cpp
#include <iostream>
using namespace std;
// Beta is contained within Alpha. Beta exists independent of Alpha.
// UML relationship: Aggregation
class Beta
{
private:
double cost;
public:
Beta(double a_cost) : cost(a_cost) {}
void display() { cout << "cost: " << cost << endl; }
};
// Gamma is contained within Alpha. Gamma exists within Alpha.
// UML relationship: Composition
class Gamma
{
private:
int id;
public:
Gamma(int a_id) : id(a_id) {}
void display() { cout << "id: " << id << endl; }
};
// Alpha contains Beta and Gamma.
// Beta exists outside Alpha. (Aggregation)
// Gamma exists within Alpha. (Composition)
// UML relationships: Aggregation, Composition
class Alpha // must be after Beta and Gamma
{
private:
string name;
char part;
Gamma gamma; // composition
Beta* beta; // aggregation
public:
Alpha(string a_name, char a_part, int a_id)
: name(a_name), part(a_part), beta(NULL), gamma(a_id) {}
~Alpha()
{
if (beta != NULL) delete beta;
}
void setBeta(double a_cost)
{
if (beta != NULL) delete beta;
beta = new Beta(a_cost);
}
void display()
{
cout << "name: " << name << endl;
cout << "part: " << part << endl;
gamma.display();
if (beta != NULL) {
beta->display();
}
}
};
// Delta inherits Alpha
// UML relationship: Inheritance or Generalization
class Delta : public Alpha
{
private:
string desc;
public:
Delta(string a_name, char a_part, int a_id, string a_desc)
: Alpha(a_name, a_part, a_id), desc(a_desc) {}
void display()
{
Alpha::display();
cout << "desc: " << desc << endl;
}
};
int main()
{
// Instantiate a Delta, set Beta and then display Delta
Delta delta("Myproduct", 'a', 100, "This is a new product");
delta.setBeta(10);
delta.display();
return 0;
}
Compilation: g++ Greek.cpp
Sample Execution Output: ./a.out
name: Myproduct
part: a
id: 100
cost: 10
desc: This is a new product
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.