大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include <string>
#include "dma.h"
using namespace std;
const int CLIENTS = 4;
int main()
{
ABC* p_clients[CLIENTS];
char kind;
for (int i = 0; i < CLIENTS; i++)
{
cout << "Select 1)ABC, 2)baseDMA, 3)lacksDMA, 4)hasDMA:";
while (cin>>kind&&(kind!='1'&&kind!='2'&&kind!='3'&&kind!='4'))
{
cout << "Enter either 1 2 3 4 or 4:";
}
if (kind == '1')
{
p_clients[i] = new ABC();
}
else if (kind == '2')
{
while (cin.get() != '\n')continue;
char l[40];
int r;
cout << "Enter baseDMA's label:";
cin.getline(l, 40);
cout << "Enter baseDMA's rating:";
cin >> r;
p_clients[i] = new baseDMA(l, r);
}
else if (kind == '3')
{
while (cin.get() != '\n')continue;
char l[40], c[40];
int r;
cout << "Enter lacksDMA's label:";
cin.getline(l, 40);
cout << "Enter lacksDMA's color:";
cin.getline(c, 40);
cout << "Enter lacksDMA's rating:";
cin >> r;
p_clients[i] = new lacksDMA(c, l, r);
}
else if (kind == '4')
{
while (cin.get() != '\n')continue;
char l[40], s[40];
int r;
cout << "Enter hassDMA's label:";
cin.getline(l, 40);
cout << "Enter hasDMA's style:";
cin.getline(s, 40);
cout << "Enter hasDMA's rating:";
cin >> r;
p_clients[i] = new hasDMA(s, l, r);
}
while (cin.get()!='\n')
{
continue;
}
}
cout << endl;
for (int i = 0; i < CLIENTS; i++)
{
p_clients[i]->View();
cout << endl;
}
for (int i = 0; i < CLIENTS; i++)
{
delete p_clients[i];
}
cout << "DONE" << endl;
return 0;
}
dma.h
#pragma once
#include <iostream>
using namespace std;
class ABC
{
public:
virtual ~ABC(){};
virtual void View() { cout << "This is ABC View(),it is empty.\n"; }
};
class baseDMA:public ABC
{
private:
char* label;
int rating;
public:
baseDMA(const char* l = "null", int r = 0);
baseDMA(const baseDMA& rs);
virtual ~baseDMA();
virtual void View()override;
baseDMA& operator=(const baseDMA& rs);
friend ostream& operator<<(ostream& os, const baseDMA& rs);
};
class lacksDMA :public baseDMA
{
private:
enum{COL_LEN=40};
char color[COL_LEN];
public:
lacksDMA(const char* c = "blank", const char* l = "nnull", int r = 0);
lacksDMA(const char* c, const baseDMA& rs);
virtual void View()override;
friend ostream& operator<<(ostream& os, const lacksDMA& ls);
};
class hasDMA :public baseDMA
{
private:
char* style;
public:
hasDMA(const char* s = "none", const char* l = "null",int r=0);
hasDMA(const char* s, const baseDMA& rs);
hasDMA(const hasDMA& hs);
~hasDMA();
virtual void View()override;
hasDMA& operator=(const hasDMA& hs);
friend ostream& operator<<(ostream& os, const hasDMA& hs);
};
dma.cpp
#include "dma.h"
baseDMA::baseDMA(const char* l, int r)
{
label = new char[strlen(l) + 1];
strcpy_s(label, strlen(l) + 1,l);
rating = r;
}
baseDMA::baseDMA(const baseDMA& rs)
{
label = new char[strlen(rs.label) + 1];
strcpy_s(label, strlen(rs.label) + 1, rs.label);
rating = rs.rating;
}
baseDMA::~baseDMA()
{
delete[] label;
}
void baseDMA::View()
{
cout << "Now in baseDMA" << endl;
cout << *this << endl;
}
baseDMA& baseDMA::operator=(const baseDMA& rs)
{
if (this == &rs)return *this;
if (label)delete[] label;
label = new char[strlen(rs.label) + 1];
strcpy_s(label, strlen(rs.label) + 1, rs.label);
rating = rs.rating;
return *this;
}
ostream& operator<<(ostream& os, const baseDMA& rs)
{
os << "label: " << rs.label << "\trating:" << rs.rating;
return os;
}
lacksDMA::lacksDMA(const char* c, const char* l , int r ):baseDMA(l,r)
{
strcpy_s(color, COL_LEN, c);
if (strlen(c) >= COL_LEN)color[COL_LEN - 1] = '\0';
else color[strlen(c)] = '\0';
}
lacksDMA::lacksDMA(const char* c, const baseDMA& rs) :baseDMA(rs)
{
strcpy_s(color, COL_LEN, c);
if (strlen(c) >= COL_LEN)color[COL_LEN - 1] = '\0';
else color[strlen(c)] = '\0';
}
void lacksDMA::View()
{
cout << "Now in lacksDMA." << endl;
cout << *this << endl;
}
ostream& operator<<(ostream& os, const lacksDMA& ls)
{
os << (const baseDMA&)ls;
os << "\tColor:" << ls.color;
return os;
}
hasDMA::hasDMA(const char* s, const char* l, int r):baseDMA(l,r)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char* s, const baseDMA& rs):baseDMA(rs)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA& hs):baseDMA(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
void hasDMA::View()
{
cout << "Now in hasDMA." << endl;
cout << *this << endl;
}
hasDMA& hasDMA::operator=(const hasDMA& hs)
{
if (this == &hs)return *this;
baseDMA::operator=(hs);
delete[]style;
style = new char[strlen(hs.style) + 1];
strcpy_s(style, strlen(hs.style) + 1, hs.style);
return *this;
}
ostream& operator<<(ostream& os, const hasDMA& hs)
{
os << (const baseDMA&)hs;
os << "\tStyle:" << hs.style;
return os;
}
运行结果:
考查点:
- 继承
- 多态
注意:
- 父类的函数参数可以传子类
2024年9月9日15:14:58