我用字符串做了一个C++构造函数,但它不会打印出字符串
I made a C++ constructor with a string, but it won't print the string out
我做的是下面我创建了一个名为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;
}
相关文章:
- 在c++构造函数中使用随机字符串生成器
- c++ 构造函数 将 1 个字符串参数转换为 3 个属性
- C 字符串在显式构造函数中分配值C++?
- 我用字符串做了一个C++构造函数,但它不会打印出字符串
- C++继承:构造函数中的字符串构造
- 在派生自线程类的构造函数中传递字符串
- 性能 - 使用字符串构造函数与使用串联
- 字符串构造函数将两个字符* 放入另一个 std::string 在 C++14 中有效,但不适用于 C++17
- 调用哪个字符串构造函数
- 使用子字符串构造函数构建字符串 - 调试 - 当自动跳过代码时
- 使用字符串构造函数从wstring转换为字符串时,是否没有可能的数据丢失
- 不分配任何字符内存的常量字符串构造函数?
- 常量 char* 的字符串构造函数更改常量 char* 的值
- 为什么从std::string的initializer列表中填充std::向量不会调用std::字符串构造函数
- 为什么 C++03 文件流不接受字符串构造函数参数?
- 带有char*内存泄漏的字符串构造函数
- BigNum类字符串构造函数错误
- CPP字符串构造函数查找并附加空字符
- c++的字符串构造函数
- c++ std::复制结果不同于字符串构造函数