子类构造函数不存储对象的数据
Subclass constructor not storing data for object
我正在尝试创建超类 'Entry' 的子类,它存储例如借阅的图书馆项目的信息。使用我的 main 来测试这些结果 return 没有值 'name' 和 'artist' 但它确实 return 超类变量 'borrowedBy' 的值。我认为将它们放在构造函数中会将值传递给对象,但我似乎在某处犯了一个错误。
头文件:
#include <iostream>
#include <string>
class Entry{
private:
int borrowed;
std::string borrowedBy;
public:
Entry();
void printDetails();
std::string getborrowedBy();
};
class MusicAlbum : public Entry{
private:
std::string name;
std::string artist;
public:
void printDetails();
MusicAlbum(std::string name, std::string artist);
~MusicAlbum();
std::string getname();
};
cpp 文件:
#include <iostream>
#include <string>
#include "C2.h"
MusicAlbum::MusicAlbum(std::string name, std::string artist){
std::cout << "Constructor..." << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Artist: " << artist << std::endl << std::endl;
}
MusicAlbum::~MusicAlbum(){};
std::string MusicAlbum::getname(){
return name;
};
std::string Entry::getborrowedBy(){
return borrowedBy;
};
Entry::Entry(){
borrowed = 1;
borrowedBy = "test1";
};
void MusicAlbum::printDetails(){
std::cout << "Printer..." << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Artist: " << artist << std::endl << std::endl;
}
int main(){
MusicAlbum MA1("Name1", "Artist1");
MA1.printDetails();
std::cout << "getname: " << MA1.getname() << std::endl << std::endl;
std::cout << "getborrowedby: " << MA1.getborrowedBy() << std::endl << std::endl;
system("pause");
return 0;
}
运行 程序给出以下内容:
Constructor...
Name: Name1
Artist: Artist1
Printer...
Name:
Artist:
getname:
getborrowedby: test1
因此,似乎正在存储来自 'Entry' 构造函数的信息,而不是来自 'MusicAlbum' 构造函数的信息。
其次,在定义一个新对象时,是否可以在保持私有的同时更改值 'borrowedBy' ?在 MusicAlbum 构造函数中是'inaccessible'。
您必须更改构造函数以初始化 class 成员变量。由于变量名称,不会自动执行任何操作:
MusicAlbum::MusicAlbum(std::string name, std::string artist)
: name(name), artist(artist) {
...
}
name(name)
表示用参数name
(内部名称)初始化class成员name
(外部名称)。
请注意,此初始化列表还允许为基数 class 指定参数。例如,如果您有一个基础构造函数:
Entry::Entry(std::string borrower)
: borrowedBy(borrower), borrowed(1)
{ };
你还可以有一个派生构造函数,如:
MusicAlbum::MusicAlbum(std::string name, std::string artist, std::string bywhom)
: name(name), artist(artist), Entry (bywhom) {
...
}
我正在尝试创建超类 'Entry' 的子类,它存储例如借阅的图书馆项目的信息。使用我的 main 来测试这些结果 return 没有值 'name' 和 'artist' 但它确实 return 超类变量 'borrowedBy' 的值。我认为将它们放在构造函数中会将值传递给对象,但我似乎在某处犯了一个错误。 头文件:
#include <iostream>
#include <string>
class Entry{
private:
int borrowed;
std::string borrowedBy;
public:
Entry();
void printDetails();
std::string getborrowedBy();
};
class MusicAlbum : public Entry{
private:
std::string name;
std::string artist;
public:
void printDetails();
MusicAlbum(std::string name, std::string artist);
~MusicAlbum();
std::string getname();
};
cpp 文件:
#include <iostream>
#include <string>
#include "C2.h"
MusicAlbum::MusicAlbum(std::string name, std::string artist){
std::cout << "Constructor..." << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Artist: " << artist << std::endl << std::endl;
}
MusicAlbum::~MusicAlbum(){};
std::string MusicAlbum::getname(){
return name;
};
std::string Entry::getborrowedBy(){
return borrowedBy;
};
Entry::Entry(){
borrowed = 1;
borrowedBy = "test1";
};
void MusicAlbum::printDetails(){
std::cout << "Printer..." << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Artist: " << artist << std::endl << std::endl;
}
int main(){
MusicAlbum MA1("Name1", "Artist1");
MA1.printDetails();
std::cout << "getname: " << MA1.getname() << std::endl << std::endl;
std::cout << "getborrowedby: " << MA1.getborrowedBy() << std::endl << std::endl;
system("pause");
return 0;
}
运行 程序给出以下内容:
Constructor...
Name: Name1
Artist: Artist1
Printer...
Name:
Artist:
getname:
getborrowedby: test1
因此,似乎正在存储来自 'Entry' 构造函数的信息,而不是来自 'MusicAlbum' 构造函数的信息。 其次,在定义一个新对象时,是否可以在保持私有的同时更改值 'borrowedBy' ?在 MusicAlbum 构造函数中是'inaccessible'。
您必须更改构造函数以初始化 class 成员变量。由于变量名称,不会自动执行任何操作:
MusicAlbum::MusicAlbum(std::string name, std::string artist)
: name(name), artist(artist) {
...
}
name(name)
表示用参数name
(内部名称)初始化class成员name
(外部名称)。
请注意,此初始化列表还允许为基数 class 指定参数。例如,如果您有一个基础构造函数:
Entry::Entry(std::string borrower)
: borrowedBy(borrower), borrowed(1)
{ };
你还可以有一个派生构造函数,如:
MusicAlbum::MusicAlbum(std::string name, std::string artist, std::string bywhom)
: name(name), artist(artist), Entry (bywhom) {
...
}