我#include <string>对程序开始时的使用感到困惑.例如,在下面的代码中,我不使用,#include <string>但该函数在运行时仍会打印出字符串"Johnny的最爱号码".
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
Run Code Online (Sandbox Code Playgroud)
但是,在下面的代码中,它确实包含#include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我只#include <string>在变量表示字符串时使用吗?
我正在编写一个程序来创建指向对象的指针向量。
如何访问指针从指向对象的指针向量中引用的各个对象?
我正在尝试为向量内的指针引用的每个对象调用类 Object 中的 speak() 函数。
感谢您的时间
class Object
{
public:
void speak()
{
cout<<"Hello!"<<endl;
}
};
int main()
{
int choice;
vector<Obj*> objVector; //create empty vector of "pointer-to-object"
Object* ptrObj; //point to object
while (choice!=5)
{
cout <<"1.Create Object\n";
cout <<"2.Destroy Object\n";
cout <<"3.Print number of existing Objects\n";
cout <<"4.Tell existing Objects to say Hello\n";
cout <<"5.Quit Program"<<endl;
cout <<"Please enter your choice: ";
cin >> choice;
if (choice==5)
cout <<"\nProgram is quitting\n"<<endl;
else if (choice==1)
{
ptrObj= …Run Code Online (Sandbox Code Playgroud) <dl>
<dt>Term 1</dt>
<dd>Definition of term 1</dd>
<dt>Term 2</dt>
<dd>Definition of term 2</dd>
<dt> Term3 </dt>
<dd> Definition of term 3</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我试图得到它,以便如果我将鼠标悬停在dt部分上,dd部分的背景颜色将变为黑色(或任何颜色).到目前为止,我只能让悬停部分改变背景颜色,但我不确定如何进行另一部分更改.