大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include "port.h"
int main() {
Port p1;
Port p2("Abc", "Bcc", 30);
std::cout << p1 << std::endl<<endl;
std::cout << p2 << std::endl << endl;
Port p3 = p2;
p3.Show();
p3 += 3;
p3.Show();
Port p4 = p2;
p3 -= 2;
p3.Show();
cout << endl;
VintagePort vp1("Vabc", 50, "hn", 1983);
vp1.Show();
cout << endl;
VintagePort vp2;
vp2.Show();
cout << endl;
vp1 -= 3;
vp2 = vp1;
std::cout << vp2 << std::endl;
return 0;
}
port.h
#pragma once
#include <iostream>
using namespace std;
class Port
{
private:
char* brand;
char style[20];
int bottles;
public:
Port(const char* br = "none", const char* st = "none", int b = 0);
Port(const Port& p);
virtual ~Port() { delete[] brand; }
Port& operator=(const Port& p);
Port& operator+=(int b);
Port& operator-=(int b);
int BottleCount()const { return bottles; }
virtual void Show()const;
friend ostream& operator<<(ostream& os, const Port& p);
};
class VintagePort :public Port
{
private:
char* nickname;
int year;
public:
VintagePort();
VintagePort(const char*br,int b,const char*nn,int y);
VintagePort(const VintagePort&vp);
~VintagePort() { delete[] nickname; }
VintagePort& operator=(const VintagePort&vp);
void Show()const override;
friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
port.cpp
#include "port.h"
Port::Port(const char* br, const char* st, int b)
{
brand = new char[strlen(br) + 1];
strcpy_s(brand, strlen(br) + 1, br);
strcpy_s(style, strlen(br) + 1, st);
bottles = b;
}
Port::Port(const Port& p)
{
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, strlen(p.style) + 1, p.style);
bottles = p.bottles;
}
Port& Port::operator=(const Port& p)
{
if (this == &p)return *this;
if (brand)delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, strlen(p.style) + 1, p.style);
bottles = p.bottles;
return *this;
}
Port& Port::operator+=(int b)
{
this->bottles += b;
return *this;
}
Port& Port::operator-=(int b)
{
this->bottles -= b;
return *this;
}
void Port::Show()const
{
cout << "Brand:" << this->brand << endl;
cout << "Kind:" << this->style << endl;
cout << "Bottles:" << this->bottles << endl;
}
ostream& operator<<(ostream& os, const Port& p)
{
os << p.brand << "," << p.style << "," << p.bottles;
return os;
}
VintagePort::VintagePort():Port()
{
nickname = new char[1];
nickname[0] = '\0';
year = 0;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y):Port(br,"none",b)
{
nickname = new char[strlen(nn) + 1];
strcpy_s(nickname, strlen(nn) + 1, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
year = vp.year;
}
VintagePort& VintagePort::operator=(const VintagePort& vp)
{
if (this == &vp)return *this;
if (nickname)delete[] nickname;
Port::operator=(vp);
nickname = new char[strlen(vp.nickname) + 1];
strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show()const
{
Port::Show();
cout << "nickname:" << nickname << endl;
cout << "year:" << year << endl;
}
ostream& operator<<(ostream& os, const VintagePort& vp)
{
os << (const Port&)vp << "," << vp.nickname << "," << vp.year << endl;
return os;
}
运行结果:
考查点:
- 继承
- 虚函数
2024年9月9日16:05:29