2023级cpp第二学期上机练习题第5次(对象数组和对象指针)

文章讲述了如何在C++中使用对象数组和对象指针,实现类的实例化、构造函数调用、成员函数展示,并通过示例演示了距离计算和角色互动的场景。
摘要由CSDN通过智能技术生成

题源地址

1:对象数组-1

描述

把代码补充完整,以实现要求的输出。

主函数如下:

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;
}
 

输入

输出

以下文字,最后一行后面也要换行。

Mercury's constructor. Distance between Mercury and Sun is 57910000 km.
Venus's constructor. Distance between Venus and Sun is 108200000 km.
Earth's constructor. Distance between Earth and Sun is 149600000 km.
Mars's constructor. Distance between Mars and Sun is 227940000 km.
Jupiter's constructor. Distance between Jupiter and Sun is 778330000 km.
Saturn's constructor. Distance between Saturn and Sun is 1429400000 km.
Distance between Mercury and Earth is 91690000 km.
Distance between Earth and Mars is 78340000 km.

样例输入

样例输出

Mercury's constructor. Distance between Mercury and Sun is 57910000 km.
Venus's constructor. Distance between Venus and Sun is 108200000 km.
Earth's constructor. Distance between Earth and Sun is 149600000 km.
Mars's constructor. Distance between Mars and Sun is 227940000 km.
Jupiter's constructor. Distance between Jupiter and Sun is 778330000 km.
Saturn's constructor. Distance between Saturn and Sun is 1429400000 km.
Distance between Mercury and Earth is 91690000 km.
Distance between Earth and Mars is 78340000 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、把代码补充完整(共两处,分别标有//①和//②),以实现如下要求的输出。根据提示写代码,其他地方不得添加代码。
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
 string name;
 int score;
 //①这里应当是构造函数
};
//②这里应当是show函数的定义
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;
}

输入

输出

Tom's score is 80.
Jerry's score is 95.
Garfield's score is 85.
Tom's score is 15 lower than Jerry's.
Jerry's score is 10 higher than Garfield's.
 

样例输入

样例输出

Tom's score is 80.
Jerry's score is 95.
Garfield's score is 85.
Tom's score is 15 lower than Jerry's.
Jerry's score is 10 higher than Garfield's.

分析:

        依旧是老朋友

代码:

#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>
#include<string>
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] { { "AAA",80 },{ "BBB",90 },{ "CCC",100 } }; //把这句改写成用对象指针new 3个对象
 for (int i = 0; i < 3; i++)
 {
  p[i].print(); //这句也改成用指针访问
 }
 return 0;
}
 

输入

输出

以下文字,最后一行后面也要换行。

AAA 80
BBB 90
CCC 100

 

样例输入

样例输出

AAA 80
BBB 90
CCC 100

分析:

        熟悉的指针朋友又回来了!为其打响迎接的礼炮吧!

代码:

#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()
{
 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;
}

输入

输出

以下文字,最后一行后面也要换行。

Pretty Goat is a goat that weights 47 kg.
Warm Goat is a goat that weights 60 kg.
Wilie is a wolf that weights 50 kg.
Warm Goat has a good sleep, increasing 2 kg.
Wilie eats Pretty Goat, increasing 47 kg
Warm Goat is a goat that weights 62 kg.
Wilie is a wolf that weights 97 kg.


 

样例输入 

样例输出

Pretty Goat is a goat that weights 47 kg.
Warm Goat is a goat that weights 60 kg.
Wilie is a wolf that weights 50 kg.
Warm Goat has a good sleep, increasing 2 kg.
Wilie eats Pretty Goat, increasing 47 kg
Warm Goat is a goat that weights 62 kg.
Wilie is a wolf that weights 97 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()
{
 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;
}

输入

输出

以下文字,最后一行后面也要换行。

Tom HP: 100 ATTACK: 10
Garfield HP: 50 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 80 ATTACK: 10
Garfield HP: 40 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 60 ATTACK: 10
Garfield HP: 30 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 40 ATTACK: 10
Garfield HP: 20 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 20 ATTACK: 10
Garfield HP: 10 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield is destroyed.
 

样例输入

样例输出

Tom HP: 100 ATTACK: 10
Garfield HP: 50 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 80 ATTACK: 10
Garfield HP: 40 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 60 ATTACK: 10
Garfield HP: 30 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 40 ATTACK: 10
Garfield HP: 20 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield attacks Tom. Tom receives 20 damage.
----------
Tom HP: 20 ATTACK: 10
Garfield HP: 10 ATTACK: 20
**********
Tom attacks Garfield. Garfield receives 10 damage.
Garfield is destroyed.
 

分析:

       没有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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值