大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include "person.h"
using namespace std;
int main()
{
Person person("Jakey", "Slong");
person.show();
Gunslinger g1("Tidy", "white", 12);
g1.show();
cout << "Gunslinger's nick is " << g1.Draw() << endl;
PokerPlayer pokerplayer;
pokerplayer.show();
return 0;
}
person.h
#pragma once
#include <iostream>
using namespace std;
class Person
{
private:
string fname;
string lname;
public:
Person() :fname("no name"), lname("no name") {};
Person(string f, string l);
virtual ~Person();
virtual void show()const;
};
class Gunslinger :virtual public Person
{
private:
int nick;
public:
Gunslinger() :Person(), nick(0) {};
Gunslinger(string f, string l, int n);
~Gunslinger();
double Draw()const;
void show()const override;
};
struct Card
{
enum SUITE{SPADE,HEART,DIAMOND,CLUB};
SUITE suite;
int number;
};
class PokerPlayer :virtual public Person
{
public:
~PokerPlayer() {};
Card Draw()const;
};
class BadDude :public Gunslinger, public PokerPlayer
{
public:
double GDraw()const;
int CDraw()const;
void show()const override;
};
person.cpp
#include "person.h"
#include <ctime>
#include <cstdlib>
Person::Person(string f, string l):fname(f),lname(l){}
Person::~Person() {};
void Person::show()const
{
cout << fname << "." << lname << endl;
}
Gunslinger::Gunslinger(string f, string l, int n):Person(f,l),nick(n) {}
Gunslinger::~Gunslinger(){}
double Gunslinger::Draw()const
{
srand(time(0));
return rand() % 60;
}
void Gunslinger::show()const
{
Person::show();
cout << "Nick:" << nick << endl;
}
Card PokerPlayer::Draw()const
{
Card temp;
srand(time(0));
temp.number = rand () % 54+1;
temp.suite = Card::SUITE(rand() % 4);
return temp;
}
double BadDude::GDraw()const
{
double temp=Gunslinger::Draw();
return temp;
}
int BadDude::CDraw()const
{
int temp = PokerPlayer::Draw().number;
return temp;
}
void BadDude::show()const
{
Gunslinger::show();
PokerPlayer::show();
}
运行结果:
考查点:
- 菱形继承
- 虚继承
- 作用域
- 虚函数
2024年9月11日09:00:57