大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
#include <iostream>
#include "classic.h"using namespace std;
void Bravo(const Cd& disk);int main()
{Cd c1("Beatles", "Capitol", 14, 35.5);Classic c2 = Classic("Piano Sonqtq in B flat,Fantasia in c", "Alfred Brendel", "Philips", 2, 57.17);Cd* pcd = &c1;cout << "Using object directly:" << endl;c1.Report();c2.Report();cout << endl;cout << "Using type cd *pointer to objects:\n";pcd->Report();pcd = &c2;pcd->Report();cout << endl;cout << "Calling a function with a Cd reference argument:" << endl;Bravo(c1);Bravo(c2);cout << endl;cout << "Testing assignment: ";Classic copy;copy = c2;copy.Report();return 0;
}void Bravo(const Cd& disk)
{disk.Report();
}
classic.h
#pragma once
#include <iostream>
using namespace std;class Cd
{
private:char *performers;char *label;int selections;double playtime;
public:Cd(const char* s1, const char* s2, int n, double x);Cd(const Cd& d);Cd();virtual ~Cd();virtual void Report()const;Cd& operator=(const Cd& d);
};class Classic :public Cd
{
private:char *works;
public:Classic(const char* s1, const char* s2, const char* s3, int n, double x);Classic();Classic(const Classic& c);~Classic();virtual void Report()const override;Classic& operator=(const Classic& c);
};
classic.cpp
#include "classic.h"Cd::Cd(const char* s1, const char* s2, int n, double x)
{performers = new char[strlen(s1) + 1];strcpy_s(performers, strlen(s1) + 1, s1);if (strlen(s1) >= 50)performers[49] = '\0';else performers[strlen(s1)] = '\0';label = new char[strlen(s2) + 1];strcpy_s(label, strlen(s2) + 1, s2);if (strlen(s2) >= 20)label[19] = '\0';else label[strlen(s2)] = '\0';selections = n;playtime = x;
}
Cd::Cd(const Cd& d)
{performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;
}
Cd::Cd()
{performers = new char[1];performers[0] = '\0';label = new char[1];label[0] = '\0';selections = 0;playtime = 0;
}
Cd::~Cd()
{delete[] performers;delete[] label;
}
void Cd::Report()const
{cout << "Performers:" << performers << endl;cout << "Label:" << label << endl;cout << "Selecttions:" << selections << endl;cout << "Playtime:" << playtime << endl;
}
Cd& Cd::operator=(const Cd& d)
{if (this == &d)return *this;if (performers)delete[] performers;if (label)delete[] label;performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;return *this;
}Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x) :Cd(s1, s2, n, x)
{works = new char[strlen(s3) + 1];strcpy_s(works, strlen(s3) + 1, s3);if (strlen(s3) >= 50)works[49] = '\0';else works[strlen(s3)] = '\0';
}
Classic::Classic() :Cd()
{works = new char[1];works[0] = '\0';
}
Classic::Classic(const Classic& c) :Cd(c)
{works = new char[strlen(c.works) + 1];strcpy_s(works, strlen(c.works) + 1, c.works);
}
Classic::~Classic()
{delete[] works;
}
void Classic::Report()const
{Cd::Report();cout << "Works:" << works << endl;
}
Classic& Classic::operator=(const Classic& c)
{if (this == &c)return *this;Cd::operator=(c);if (works)delete[] works;works = new char[strlen(c.works)+1];strcpy_s(works, strlen(c.works) + 1, c.works);return *this;
}
运行结果:
考查点:
- 动态内存分配
- 继承
注意:
- 继承中的析构要加
virtual
- 多态的实现要在继承中,且需要虚函数,在父类指针或引用指向子类对象时生效.
2024年9月9日09:12:41