题源地址
1:对象数组-1
描述 |
---|
把代码补充完整,以实现要求的输出。 主函数如下: int main() |
输入 |
无 |
输出 |
以下文字,最后一行后面也要换行。 Mercury's constructor. Distance between Mercury and Sun is 57910000 km. |
样例输入 |
样例输出 |
Mercury's constructor. Distance between Mercury and Sun is 57910000 km. |
分析:
给函数传对象需取地址
代码:
#include <bits/stdc++.h>
using namespace std;
class Planet
{
public:
string name;
long long distance;
Planet(string name, long long distance) : name(name), distance(distance)
{
cout << name << "'s constructor. Distance between " << name
<< " and Sun is " << distance << " km." << endl;
}
long long getDistance(Planet &secondPlant) { return abs(distance - secondPlant.distance); }
};
void showDistance(Planet &firstPlant, Planet &secondPlant)
{
cout << "Distance between " << firstPlant.name << " and " << secondPlant.name
<< " is " << firstPlant.getDistance(secondPlant) << " km." << endl;
}
int main()
{
Planet p[] = {{"Mercury", 57910000},
{"Venus", 108200000},
{"Earth", 149600000},
{"Mars", 227940000},
{"Jupiter", 778330000},
{"Saturn", 1429400000}};
showDistance(p[0], p[2]);
showDistance(p[2], p[3]);
return 0;
}
2:对象数组-2
描述 |
---|
把代码补充完整(共两处,分别标有//①和//②),以实现如下要求的输出。根据提示写代码,其他地方不得添加代码。 1、把代码补充完整(共两处,分别标有//①和//②),以实现如下要求的输出。根据提示写代码,其他地方不得添加代码。 |
输入 |
无 |
输出 |
Tom's score is 80. |
样例输入 |
样例输出 |
Tom's score is 80. |
分析:
依旧是老朋友
代码:
#include <bits/stdc++.h>
using namespace std;
class Student
{
public:
string name;
int score;
// ①这里应当是构造函数
Student(string name, int score) : name(name), score(score)
{
cout << name << "'s score is " << score << "." << endl;
}
};
// ②这里应当是show函数的定义
void show(Student firstStudent, Student secondStudent)
{
if (firstStudent.score > secondStudent.score)
{
cout << firstStudent.name << "'s score is " << firstStudent.score - secondStudent.score
<< " higher than " << secondStudent.name << "'s." << endl;
}
else if (firstStudent.score < secondStudent.score)
{
cout << firstStudent.name << "'s score is " << secondStudent.score - firstStudent.score
<< " lower than " << secondStudent.name << "'s." << endl;
}
else
{
cout << firstStudent.name << "'s score is the same as " << secondStudent.name << "'s." << endl;
}
}
int main()
{
Student s[] = {Student("Tom", 80),
Student("Jerry", 95),
Student("Garfield", 85)};
show(s[0], s[1]);
show(s[1], s[2]);
return 0;
}
3:对象指针-1
描述 |
---|
把以下代码的主程序部分的对象数组定义,改造成用对象指针定义。不要修改Student类。 #include<iostream> |
输入 |
无 |
输出 |
以下文字,最后一行后面也要换行。 AAA 80 |
样例输入 |
样例输出 |
AAA 80 |
分析:
熟悉的指针朋友又回来了!为其打响迎接的礼炮吧!
代码:
#include <bits/stdc++.h>
using namespace std;
class Student
{
private:
string name;
int score;
public:
Student(string name, int score) : name(name), score(score) {}
void print()
{
cout << name << " " << score << endl;
}
};
int main()
{
Student *p[3] = {new Student("AAA", 80), new Student("BBB", 90), new Student("CCC", 100)};
for(auto& i : p)
i->print();
return 0;
}
4:对象指针-2
描述 |
---|
把代码补充完整,以实现要求的输出。 主函数如下: int main() |
输入 |
无 |
输出 |
以下文字,最后一行后面也要换行。 Pretty Goat is a goat that weights 47 kg.
|
样例输入 |
样例输出 |
Pretty Goat is a goat that weights 47 kg. |
分析:
理解指针,成为指针。记得指针调用成员用 ->
代码:
#include <bits/stdc++.h>
using namespace std;
class Goat
{
public:
string name;
int weight;
Goat(string n, int w) : name(n), weight(w) {}
void sleep()
{
cout << name << " has a good sleep, increasing 2 kg." << endl;
weight += 2;
}
void print() const
{
cout << name << " is a goat that weights " << weight << " kg." << endl;
}
string getName() const
{
return name;
}
int getWeight() const
{
return weight;
}
};
class Wolf
{
public:
string name;
int weight;
Wolf(string t, int w) : name(t), weight(w) {}
void eat(Goat *p)
{
cout << name << " eats " << p->getName() << ", increasing " << p->getWeight() << " kg" << endl;
weight += p->getWeight();
}
string getName() const
{
return name;
}
int getWeight() const
{
return weight;
}
};
void print(Goat *p)
{
cout << p->getName() << " is a goat that weights " << p->getWeight() << " kg." << endl;
}
void print(Wolf *p)
{
cout << p->getName() << " is a wolf that weights " << p->getWeight() << " kg." << endl;
}
int main()
{
Goat *g1 = new Goat("Pretty Goat", 47);
Goat *g2 = new Goat("Warm Goat", 60);
Wolf *w1 = new Wolf("Wilie", 50);
print(g1);
print(g2);
print(w1);
g2->sleep();
w1->eat(g1);
print(g2);
print(w1);
return 0;
}
5:类的综合题-6
描述 |
---|
Tom猫和Garfield猫打起来了。 把代码补充完整,以实现要求的输出。 主函数如下: int main() |
输入 |
无 |
输出 |
以下文字,最后一行后面也要换行。 Tom HP: 100 ATTACK: 10 |
样例输入 |
样例输出 |
Tom HP: 100 ATTACK: 10 |
分析:
没有BUG的代码是不完美的! ---Sodium_Sulfate
代码:
#include <bits/stdc++.h>
using namespace std;
class Cat
{
private:
string name;
int HP, ATTACK;
public:
Cat(string n, int h, int a) : name(n), HP(h), ATTACK(a) {}
void showIntro() const
{
cout << name << " HP: " << HP << " ATTACK: " << ATTACK << endl;
}
bool isAlive() const { return HP; }
void attack(Cat &cat2)
{
cout << name << " attacks " << cat2.name << ". " << cat2.name << " receives " << ATTACK << " damage." << endl;
cat2.HP -= ATTACK;
if (!cat2.HP)
{
cout << cat2.name << " is destroyed." << endl;
}
}
};
int main()
{
Cat c1("Tom", 100, 10), c2("Garfield", 50, 20);
while (c1.isAlive() && c2.isAlive())
{
c1.showIntro();
c2.showIntro();
cout << "**********" << endl;
c1.attack(c2);
if (!c2.isAlive())
break;
c2.attack(c1);
cout << "----------" << endl;
}
return 0;
}