题目一:概念填空
要求 :请将左边的代码元素用通俗易懂的词语解释 class Student Student stu1 name
题目二:代码填空题
要求 :假设我们有一个 Dog 类,它有 dogName 属性和 bark() 方法。请在下面的 main 函数中填空,完成创建一只叫"旺财"的狗,并让它叫一声。
#include <iostream>
#include <string>
class Dog {
public:
std::string dogName;
void bark() {
std::cout << dogName << " says: Woof! Woof!" << std::endl;
}
};
int main() {
// 1. 根据 Dog 类(模具),创建一只具体的狗对象,取名叫 myDog
______ ______;
// 2. 给这只狗对象(具体的狗)命名为 "旺财"
______.______ = "旺财";
// 3. 调用这只狗的吠叫方法
______.______();
return 0;
}
题目三:动手编码题
要求 :现在轮到你亲手设计一个完整的类了。请编写一个 Book 类,并按要求在 main 函数中测试它。
Book 类的详细要求:
-
类名必须是 Book 。
-
它必须有两个 public 的成员变量:
- std::string title; (书名)
- std::string author; (作者)
-
它必须有一个 public 的成员函数:
- void showInfo() ,这个函数的功能是在控制台打印出书的完整信息,格式为:"Book Title: [书名], Author: [作者]"。 main 函数的详细测试要求:
-
创建一个 Book 类的对象,命名为 myBook 。
-
设置 myBook 对象的 title 为 "Effective C++"。
-
设置 myBook 对象的 author 为 "Scott Meyers"。
-
调用 myBook 对象的 showInfo() 方法,以显示这本书的信息。
题目一:概念填空 - 答案
class Student - 类(蓝图) Student stu1 - 对象(实体) name - 成员变量(属性)
题目二:代码填空题 - 答案
#include <iostream>
#include <string>
class Dog {
public:
std::string dogName;
void bark() {
std::cout << dogName << " says: Woof! Woof!" << std::endl;
}
};
int main() {
// 1. 根据 Dog 类(模具),创建一只具体的狗对象,取名叫 myDog
Dog myDog;
// 2. 给这只狗对象(具体的狗)命名为 "旺财"
myDog.dogName= "旺财";
// 3. 调用这只狗的吠叫方法
myDog.bark();
return 0;
}
题目三:动手编码题 - 答案
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Book
{
public:
string title;//书名
string author;//作者
void showInfo()
{
cout << "Book Title:" << title << ",Author:" << author;
}
};
int main(int argc, char** argv) {
Book myBook;
myBook.title = "Effective C++";
myBook.author = "Scott Meyers";
myBook.showInfo();
return 0;
}