Home > Software engineering >  Artwork label (Artist.cpp Isn't printing)
Artwork label (Artist.cpp Isn't printing)

Time:01-25

main.cpp

#include "Artist.h"
#include "Artwork.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
   string userTitle, userArtistName;
   int yearCreated, userBirthYear, userDeathYear;

   getline(cin, userArtistName);
   getline(cin, userTitle);
   cin >> userBirthYear;
   cin >> userDeathYear;
   cin >> yearCreated;

   Artist userArtist =  Artist(userArtistName, userBirthYear, userDeathYear);

   Artwork newArtwork = Artwork(userTitle, yearCreated, userArtist);

   newArtwork.PrintInfo();
}

Artwork.cpp

#include "Artwork.h"
#include <iostream>

// TODO: Define default constructor
Artwork::Artwork(){
title = "Unknown";
yearCreated = -1;
}

// TODO: Define second constructor to initialize
//       private fields (title, yearCreated, artist)
Artwork::Artwork(string title, int yearCreated, Artist artist){
this->title = title;
this->yearCreated = yearCreated;
this->artist = artist;
}
// TODO: Define get functions: GetTitle(), GetYearCreated()
string Artwork::GetTitle(){
   return title;
}
int Artwork::GetYearCreated(){
   return yearCreated;
}
// TODO: Define PrintInfo() function
//       Call the PrintInfo() function in the Artist class to print an artist's information  
void Artwork::PrintInfo(){
cout << "Title: " << title << ", " << yearCreated<< endl;
}

Artwork.h

#ifndef ARTWORKH
#define ARTWORKH

#include "Artist.h"

class Artwork{
   public:
      Artwork();

      Artwork(string title, int yearCreated, Artist artist);

      string GetTitle();

      int GetYearCreated();

      void PrintInfo();
   
   private:
      // TODO: Declare private data members - title, yearCreated
      string title;
      int yearCreated;

      // TODO: Declare private data member artist of type Artist
      Artist artist;
};

#endif

Artist.h

#ifndef ARTISTH
#define ARTISTH

#include <string>
using namespace std;

class Artist{
   public:
      Artist();

      Artist(string artistName, int birthYear, int deathYear);

      string GetName() const;

      int GetBirthYear() const;

      int GetDeathYear() const;

      void PrintInfo() const;
   
   private:
      // TODO: Declare private data members - artistName, birthYear, deathYear
      string artistName;
      int birthYear;
      int deathYear;

};

#endif

Artist.cpp

#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;

// TODO: Define default constructor
Artist::Artist(){
   artistName = "Unknown";
   birthYear = -1;
   deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
   this->artistName = artistName;
   this->birthYear = birthYear;
   this->deathYear = deathYear;
}


// TODO: Define get functions: GetName(), GetBirthYear(), GetDeathYear()
string Artist::GetName() const{
return artistName;
}
int Artist::GetBirthYear()const {
return birthYear;
}
int Artist::GetDeathYear() const{
return deathYear;
}
// TODO: Define PrintInfo() function
void Artist::PrintInfo() const{
if( birthYear >=0 && deathYear == -1){
   
cout << "Artist: " << artistName << "(" << birthYear << "to present"<< ")"<< endl;
}

else if(birthYear >=0 && deathYear >=0){
cout << "Artist: " << artistName << "(" << birthYear << "to " << deathYear << ")"<< endl;
}
else{
cout << "Artist: " << artistName << " (" << "unknown" << ")"<< endl;
}
}

Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "unknown" and the years of birth and death to -1. PrintInfo() displays "Artist:", then a space, then the artist's name, then another space, then the birth and death dates in one of three formats:

  • (XXXX to YYYY) if both the birth and death years are nonnegative

  • (XXXX to present) if the birth year is nonnegative and the death year is negative

  • (unknown) otherwise

Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors to initialize an artwork's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the title to "unknown", the year created to -1. PrintInfo() displays an artist's information by calling the PrintInfo() function in the Artist class, followed by the artwork's title and the year created. Declare a private field of type Artist in the Artwork class.

Ex: If the input is:

Pablo Picasso
Three Musicians
1881
1973
1921

1881 and 1973 being the birth and death years respectively, with 1921 being the year the work was created, the output is:

Artist: Pablo Picasso (1881 to 1973)
Title: Three Musicians, 1921

My issue Is when I test this input I only get... (in a different .h file)

Title: Distant Muses, 2000

I was wondering why void Artist::PrintInfo() wont output anything and would be great if anyone could guide me to the right direction!

Thank You in advance! enter image description here

CodePudding user response:

Reading through description, your Artwork class should contain PrintInfo() method, which on it's own should call Artist::PrintInfo() once invoked. Add printing the artist info before printing it's own info in the Artwork::PrintInfo() method and you should be good to go. It should look like this:

void Artwork::PrintInfo(){
    artist.Printinfo(); // Not 100% sure this is correct syntax, check carefully.
    cout << "Title: " << title << ", " << yearCreated<< endl;
}

Also, worth noting that you can 'merge' constructors. Instead of this(Yes I'm aware it's 'required', but you can take a step further):

Artist::Artist(){
   artistName = "Unknown";
   birthYear = -1;
   deathYear = -1;
}
// TODO: Define second constructor to initialize
//private fields (artistName, birthYear, deathYear)
Artist::Artist(string artistName, int birthYear, int deathYear){
   this->artistName = artistName;
   this->birthYear = birthYear;
   this->deathYear = deathYear;
}

You can have this:

Artist::Artist(string artistName="Unknown", int birthYear=-1, int deathYear=-1){
   this->artistName = artistName;
   this->birthYear = birthYear;
   this->deathYear = deathYear;
}

By setting default values, if constructor isn't provided any value, aka default constructor is used, everything will be set to default value, in this case "Unknown" and -1's. If someone provides name, you will have name and -1's and so on.

  •  Tags:  
  • c
  • Related