Help with editing/correcting code

Hi all,

I've completed a basic assignment for a class of mine. This is a assignment where you need to use classes to basically show a price of comuputer, monitor and the whole complect together.

Before I submit it, my teacher pointed out some mistakes but failed to properly explain it so Im a bit stuck and was hoping someone here might help me find/explain the issue im having.

The problems he raised were:

You cant use *this = Computer (name,ram,price); if you have not overloaded the '=' operator.

I did not see any compositions. (I think I fixed this by using the actual objects now, but I might be wrong)

And then he basically put exclamation points after
std::string computername_final; ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!
std::string monitorname_final;///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
in the complect class.

Heres my code:
Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}



Computer.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;


    //print computer
    void printComputer() const;

    //set computer
    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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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";
}

void Computer::setComputer(std::string name, int ram, double price){
    *this = Computer (name, ram, price); //I ASSUME THIS IS THE PROBLEM HES HAVING BUT I THOUGHT IM USING IT RIGHT
}


Monitor.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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";
}

void Monitor::setMonitor(std::string name, std::string type, double price){
    *this = Monitor (name, type, price);
}



Complect.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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() const;

    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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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() const{
    std::cout << "Complect name = " << name() <<"\n"
    << "Computer name = " <<computername() <<"\n"
    <<"Monitor name = " <<monitorname() <<"\n"
    <<"Complect price = " <<price() <<" EUR \n";
}

void Complect::setComplect(std::string name, std::string computername, std::string monitorname, double price){
    *this = Complect (name, computername, monitorname, price);
}


Im sorry if its a bit messy, but I hope this is understandable.

Any pointers/help would be really appreciated.

Thank you everyone.
> *this = Complect (name, computername, monitorname, price);
All these are redundant, since all you seem to be doing is recreating the constructors (only badly).
Just delete all those set functions with *this = inside them.

> std::string computername_final;//!!!!!!!!!!!!!!!!
> std::string monitorname_final;//!!!!!!!!!!!!!!!!!
I guess your tutor wants to see here
1
2
Computer computer;
Monitor monitor;


Which you set up in main with something like
1
2
3
4
    Computer asus("Asus One", 8, 545.95) ;
    Monitor iiyama("Iiyama Blackhawk 27inch", "LED", 299.99);
    Complect numberOne ("Number one complect", asus, iiyama);
    numberOne.printComplect();

Adjust your Complect constructor and print function to do the right things with the information contained in the classes.
Topic archived. No new replies allowed.