首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >艺术家::艺术家和艺术品::艺术品

艺术家::艺术家和艺术品::艺术品
EN

Stack Overflow用户
提问于 2022-09-23 23:02:14
回答 1查看 47关注 0票数 0

我对这个多文件类很陌生,我对我的两个类都有一个未定义的引用,我认为它与我的初始化和默认构造函数有关,但老实说,我一点也不知道这是否是正确的。任何帮助都是非常感谢的。

main.cpp

代码语言:javascript
复制
#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);
   cin >> userBirthYear;
   cin.ignore();
   cin >> userDeathYear;
   cin.ignore();
   getline(cin, userTitle);
   cin >> yearCreated;
   cin.ignore();

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

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

   newArtwork.PrintInfo();
}

Artist.h

代码语言:javascript
复制
#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:
      string artistName;
      int birthYear;
      int deathYear;

};

#endif

Artist.cpp

代码语言:javascript
复制
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;

Artist::Artist()
{
   artistName = "unknown";
   birthYear = -1;
   deathYear = -1;
}



string Artist::GetName() const 
{
   return artistName;   
}

int Artist::GetBirthYear() const
{
   return birthYear;   
}

int Artist::GetDeathYear() const
{
   return deathYear;
}

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

Artwork.h

代码语言:javascript
复制
#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:
      string title;
      int yearCreated;
  
      Artist artist;

};

#endif

Artwork.cpp

代码语言:javascript
复制
#include "Artwork.h"
#include <iostream>

Artwork::Artwork()
{
   cout <<"Artwork has started." << endl;
   title = "unknown";
   yearCreated = -1;
   Artist artist(); //here im not sure how to initialize an object inside of an object
}


string Artwork::GetTitle()
{
   return title;   
}

int Artwork::GetYearCreated()
{
   return yearCreated;   
}

void Artwork::PrintInfo()
{
   artist.PrintInfo();
   cout << "Title: " << title << ", " << yearCreated << endl;

}
EN

回答 1

Stack Overflow用户

发布于 2022-09-23 23:16:58

我是正确的,我需要初始化一个默认的(没有输入参数)构造函数和一个带有输入参数的构造函数。

若要正确编写默认和参数构造函数,请执行以下操作

代码语言:javascript
复制
Artwork::Artwork(string n, int y, Artist a)
{
   title = n;
   yearCreated = y;
   artist = a;
}//constructor with given input

Artwork::Artwork()
{
   title = "unknown";
   yearCreated = -1;
}//if input is not given for any fields, refer to this constructor

注意,默认构造函数没有对象,老实说,我不确定是否有方法初始化空白对象,如果有,为什么没有必要。

艺术家构造函数看起来是一样的,

代码语言:javascript
复制
Artist::Artist(string name, int birth, int death)
{
   artistName = name;
   birthYear = birth;
   deathYear = death;
}

Artist::Artist()
{
   artistName = "unknown";
   birthYear = -1;
   deathYear = -1;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73833698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档