Basically my issue is with compositions. I understand the principle, but I'm struggling with the execution in one of the tests.
From the code of Computer
and Monitor
below, I have to create a final class Complect
which will have its own name , the name of the computer, the name of the monitor, and a price which will be made up from the price()
functions.
Computer.h
#ifndef COMPUTER_H
#define COMPUTER_H
#include <string>
class Computer{
public:
Computer(std::string name, int ram, double price);
std::string name() const;
int ram() const;
double price() const;
void printComputer() const;
void setComputer(std::string name, int ram, double price);
private:
std::string its_name;
int ram_gb;
double cost_price;
};
#endif // COMPUTER_H
Computer.cpp
#include "Computer.h"
#include <iostream>
Computer::Computer(std::string name, int ram, double price)
: its_name(name), ram_gb(ram), cost_price(price){
}
std::string Computer::name() const {
return its_name;
}
int Computer::ram() const {
return ram_gb;
}
double Computer::price() const {
return cost_price;
}
void Computer::printComputer() const{
std::cout << "Computer name = " <<name() <<"\n"
<< "Computer RAM = " <<ram() <<" GB\n"
<< "Computer Price = " << price() <<" EUR \n";
}
Monitor.h
#ifndef MONITOR_H
#define MONITOR_H
#include <string>
class Monitor{
public:
Monitor(std::string name, std::string type, double price);
std::string name() const;
std::string type() const;
double price() const;
//print computer
void printMonitor() const;
//set computer
void setMonitor(std::string name, std::string type, double price);
private:
std::string its_name;
std::string type_set;
double cost_price;
};
#endif // MONITOR_H
Monitor.cpp
#include "Monitor.h"
#include <iostream>
Monitor::Monitor(std::string name, std::string type, double price) : its_name(name), type_set(type), cost_price(price){
}
std::string Monitor::name() const {
return its_name;
}
std::string Monitor::type() const{
return type_set;
}
double Monitor::price() const {
return cost_price;
}
void Monitor::printMonitor() const{
std::cout << "Monitor name = " <<name() <<"\n"
<< "Monitor type = " <<type() <<"\n"
<< "Monitor price = " << price() <<" EUR \n";
}
Here is the class that I have made:
Complect.h
#ifndef COMPLECT_H
#define COMPLECT_H
#include <string>
class Complect{
public:
Complect(std::string name, std::string computername, std::string monitorname, double price);
std::string name() const;
std::string computername() const;
std::string monitorname() const;
double price() const;
void printComplect();
void setComplect(std::string name, std::string computername, std::string monitorname, double price);
private:
std::string complect_name;
std::string computername_final;
std::string monitorname_final;
double cost_price;
};
#endif // COMPLECT_H
Complect.cpp
#include "Complect.h"
#include "Monitor.h"
#include "Computer.h"
#include <iostream>
Complect::Complect(std::string name, std::string computername, std::string monitorname, double price) :
complect_name(name), computername_final(computername), monitorname_final(monitorname), cost_price(price){
}
std::string Complect::name() const{
return complect_name;
}
std::string Complect::computername() const{
return computername_final;
}
std::string Complect::monitorname() const{
return monitorname_final;
}
double Complect::price() const{
return cost_price;
}
void Complect::printComplect(){
std::cout << "Complect name = " << name() <<"\n"
<< "Computer name = " <<computername() <<"\n"
<<"Monitor name = " <<monitorname() <<"\n"
<<"Complect price = " <<price() <<" EUR \n";
}
Here is how I use the classes in Main.cpp
#include <iostream>
#include "Computer.h"
#include "Monitor.h"
#include "Complect.h"
int main(){
Computer asus("Asus One", 8, 545.95) ;
asus.printComputer() ;
std::cout << "\n";
Monitor iiyama("Iiyama Blackhawk 27inch", "LED", 299.99);
iiyama.printMonitor();
std::cout <<"\n";
Complect numberOne ("Number one complect", asus.name(), iiyama.name(), iiyama.price() + asus.price());
numberOne.printComplect();
std::cout <<"\n";
system ("pause");
return 0;
}
The end result is what it should be, so this code works. But the issue with this is that it's incorrectly structured.
In the main.cpp
file you will see that the Complect
object is being created. But I currently provide all of the informations of that object at construction in main.cpp
file.
Sorry the codes a bit messy, but im trying to wrap my head around this and struggling at the moment... How to make the class in complect.cpp
file provide itself all its information ?