1

I've got a simple program where I have two classes which are Hat and Person. Each Person has a string name, a int idNum and a hat object. Each hat simply has a string of hatType and a char of hatSize. In the main method I want to simply declare 2 people and use a display method to show the information. Here's my current code, please go easy on me I'm still new to OOP in c++.

Person Class

class Person
{
  private:
    string name;
    unsigned int idNum;
    Hat myHat;

  public:
    Person(string, unsigned int, Hat);
    void display();
};


Person::Person(string personName, unsigned int personID)
{
    name = personName;
    idNum = personID;
    myHat = hat;
}

void Person::display()
{
        cout << "Given name : " << name << endl;
        cout << "Id. number : " << idNum << endl;
        hat.display();
}

Hat Class

class Hat
{
  private:
    string hatType;
    char hatSize; // S, M, L

  public:
    Hat(string,char);
    void display();
};

Hat::Hat(string _type, char _size){
    hatType = _type;
    hatSize = _size;
}

void Hat::display()
{
    cout << "Hat type   : " << hatType << endl;
    cout << "Hat size   : " << hatSize << endl;
}

Main

int main()
{

    Person personA("Alice",12321, Hat("Trilbee",'M'));

    Person personB("Bob",2324, Hat("Ferret",'S'));

    personA.display();
    personB.display();

    return 0;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108

2 Answers2

2

your Person class here:

 Person::Person(string personName, unsigned int personID) 
 {
  . ...

is not fully implemented just add the hat parameter...

Person::Person(string personName, unsigned int personID, Hat hat)
{....
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Below is the complete corrected(working) version. I have added some comments to show the changes i made.

#include <iostream>
#include <string>
using namespace std;
class Hat
{
  private:
    string hatType;
    char hatSize; // S, M, L

  public:
    Hat(string,char);
    //Added this default constructor since it won't be synthesized automatically
    Hat()
    {
        std::cout<<"Default constructor"<<std::endl;
    }
    void display();
};

Hat::Hat(string _type, char _size){
    hatType = _type;
    hatSize = _size;
}

void Hat::display()
{
    cout << "Hat type   : " << hatType << endl;
    cout << "Hat size   : " << hatSize << endl;
}
class Person
{
  private:
    string name;
    unsigned int idNum;
    Hat myHat;

  public:
    Person(string, unsigned int, Hat);
    void display();
};

//added the 3rd parameter of type Hat
Person::Person(string personName, unsigned int personID, Hat hat)
{
    name = personName;
    idNum = personID;
    myHat = hat;
}

void Person::display()
{
        cout << "Given name : " << name << endl;
        cout << "Id. number : " << idNum << endl;
        myHat.display();//changed hat.display() to myHat.display();
}

int main()
{
    Person personA("Alice",12321, Hat("Trilbee",'M'));

    Person personB("Bob",2324, Hat("Ferret",'S'));

    personA.display();
    personB.display();

    return 0;
}

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Sorry I took so long to get back to you. This is fantastic thanks so much for providing this I now understand it perfectly! – Damon O'Neil Aug 31 '21 at 03:36