我用字符串做了一个C++构造函数,但它不会打印出字符串

I made a C++ constructor with a string, but it won't print the string out

本文关键字:字符串 构造函数 打印 C++ 一个      更新时间:2023-10-16

我做的是下面我创建了一个名为TheSecondVeryBestClassEverMade的类(我已经做了以前最好的类,所以这就是为什么!(做了一个构造函数,并在参数中我做了一个字符串z。在这个构造函数的主体中,我尊敬另一个函数(它是C++的函数,Java中的方法?(并将字符串名称设置为z。

之后,我用TheSecondVeryBestClassEverMade做了一个对象,在它的参数中,我做了一个字符串,上面写着:"曼波5号是我的果酱!(因为这是一首很棒的歌,对吧?!但是现在的东西不会在终端中打印出来。

我在这里做错了什么?

这是我的 c++ 代码:

#include <iostream>
#include <string>
using namespace std;
//Constructor is a function that gets called automatically when a object is made.
// No more explicitly calling of the function, does it automatically!
class TheSecondVeryBestClassEverMade{
public:
//Constructors never have a return type so nothing gets returned in the body. 
// Constructor name == as the class name:
TheSecondVeryBestClassEverMade(string z){
//DONT print anything out in a constructor, only IDIOTS do that.
// Are you a idiot? No don't think so buddy!
//Normally its used to give variables a initial value. EXAMPLE TIME:
setTheSecondVeryBestName(z);
}
void setTheSecondVeryBestName(string bb){
name = bb;
}
string getTheSecondVeryBestName(){
return name;
}
private:
string name;
};
int main() {
TheSecondVeryBestClassEverMade bodyOder("Mambo Number 5 is my jam baby!");
return 0;
}

你做了bodyOder,并在其构造函数中给了它一个字符串,然后你正确地设置了......但是您没有任何实际打印它的代码。添加类似内容的内容

void printName(){
std::cout << "Name: " << name << std::endl;
}

并在制作bodyOder后调用它:

int main()
{
TheSecondVeryBestClassEverMade bodyOder("Mambo Number 5 is my jam baby!");
bodyOder.printName();
return 0;
}