继承与多态
继承
父子出现同名的成员问题
cpp
复制代码
#include <iostream>
using namespace std;
//父子类中出现重名成员
//定义一个父类
class Father{
public:
string name;
protected:
int pwd;
private:
int money;
public:
Father(){
cout<<"Father::构造"<<endl;
}
Father(string n,int p,int m):name(n),pwd(p),money(m){
cout<<"Father::有参"<<endl;
}
};
//定义子类
class Son:public Father{
public:
int name;//可以重名
};
class Test{
public:
Father f;
};
int main()
{
Father father;
Son son;
Test test;
//使用子类成员和父类成员的同名函数
cout<<"&son="<<&son.name<<endl;//子类name
cout<<"&father="<<&son.Father::name<<endl;///访问父类name
cout<<"&test="<<&test<<endl;
cout<<"&test.f="<<&test.f<<endl;
return 0;
}
关于继承中特殊成员函数
cpp
复制代码
#include <iostream>
using namespace std;
//父子类中出现重名成员
//定义一个父类
class Father{
public:
string name;
protected:
int pwd;
private:
int money;
public:
Father(){
cout<<"Father::构造"<<endl;
}
Father(string n,int p,int m):name(n),pwd(p),money(m){
cout<<"Father::有参"<<endl;
}
~Father(){
cout<<"析构"<<endl;
}
//拷贝构造
Father(const Father & other):name(other.name),pwd(other.pwd),money(other.money){
cout<<"Father::拷贝构造"<<endl;
}
//拷贝赋值
Father&operator=(const Father&other){
if(this!=&other){
this->name=other.name;
this->pwd=other.pwd;
this->money=other.money;
}
cout<<"Father::拷贝赋值函数"<<endl;
return *this;
}
void show(){
cout<<"Father::name = "<<name<<endl;
cout<<"Father::pwd = "<<pwd<<endl;
cout<<"Father::money = "<<money<<endl;
}
};
//定义子类
class Son:public Father{
private:
string toy;
public:
Son(){
cout<<"Son::无参构造"<<endl;
}
~Son(){cout<<"Son::析构函数"<<endl;}
Son(const Son& other):Father(other),toy(other.toy)
{
cout<<"Son::拷贝构造函数"<<endl;
}
Son(string n, int p, int m, string t): Father(n, p, m), toy(t)
{
cout<<"Son::有参构造"<<endl;
}
void show()
{
// cout<<"Father::name = "<<name<<endl;
// cout<<"Father::pwd = "<<pwd<<endl;
// cout<<"Father::money = "<<money<<endl; //子类中无法访问父类的私有成员
Father::show(); //调用父类提供的show函数
cout<<"toy = "<<toy<<endl;
}
};
int main()
{
Son s1("xx",123,5,"a");
s1.show();
Son s2(s1);
s2.show();
return 0;
}
练习:将图形类的获取周长和获取面积函数设置成虚函数,完成多态 再定义一个全局函数,能够在该函数中实现:无论传递任何图形,都可以输出传递的图形的周长和面积
cpp
复制代码
#include <iostream>
using namespace std;
class Shape{
double perimeter;
double area;
public:
//构造
Shape(){
cout<<"Shape::无参"<<endl;
}
//拷贝构造
Shape(const Shape & other):perimeter(other.perimeter),area(other.area)
{
cout<<"拷贝构造函数"<<endl;
}
//拷贝赋值
Shape & operator =(const Shape & other){
if(this==&other){
return *this;
}
perimeter=other.perimeter;
area=other.area;
return *this;
}
~Shape(){
cout<<"shape::析构"<<endl;
}
virtual void show(){
cout<<"perimeter"<<perimeter<<endl;
cout<<"area"<<area<<endl;
}
double getPer(){
return perimeter;
}
double getArea(){
return area;
}
void setPer(double p){
this->perimeter=p;
}
void setArea(double a){
this->area=a;
}
};
class Rectangle:public Shape
{
private:
int wide;
int high;
public:
Rectangle(){}
Rectangle(int w,int h):wide(w),high(h){}
~Rectangle(){}
Rectangle(const Rectangle &other):Shape(other),wide(other.wide),high(other.high){}
Rectangle & operator= (const Rectangle &other)
{
if(this != &other)
{
Shape::operator=(other);
this->wide = other.wide;
this->high = other.high;
}
return *this;
}
//重写父类虚函数
void show()
{
cout<<"矩形 长:"<<wide<<" 宽:"<<high<<endl;
cout<<"perimeter"<<get_perimeter()<<endl;
cout<<"area"<<get_area()<<endl;
}
int get_perimeter()
{
this->setPer( 2.0*(wide+high));
return Shape::getPer();
}
int get_area()
{
this->setArea( 1.0*wide*high);
return Shape::getArea();
}
};
//定义一个圆类
class Circle:public Shape
{
private:
int radius;
public:
Circle(){}
Circle(int r):radius(r){}
~Circle(){}
Circle(const Circle &other):Shape(other),radius(other.radius){}
Circle & operator= (const Circle &other)
{
if(this != &other)
{
Shape::operator=(other);
this->radius = other.radius;
}
return *this;
}
//重写父类虚函数
void show()
{
cout<<"圆形 半径:"<<radius<<endl;
cout<<"perimeter"<<get_perimeter()<<endl;
cout<<"area"<<get_area()<<endl;
}
int get_perimeter()
{
this->setPer( 2*3.14*radius);
return Shape::getPer();
}
int get_area()
{
this->setArea( 3.14*radius*radius);
return Shape::getArea();
}
};
void display( Shape & shape){
shape.show();
}
int main()
{
Rectangle rec(10,20);
Circle cir(10);
display(rec);
display(cir);
return 0;
}
手动实现一个循环顺序队列
cpp
复制代码
#include<iostream>
using namespace std;
class myqueue{
int front;//头
int rear;//尾部
int maxSize;//最大容量
int *data;//存储元素
public:
myqueue():front(0),rear(0),maxSize(0),data(NULL){
}
//有参构造最大k
myqueue(int k):front(0),rear(0),maxSize(k+1),data(new int[k+1]){
cout<<"有参构造"<<endl;
}
//拷贝构造
myqueue(const myqueue & other):front(other.front),rear(other.rear),maxSize(other.maxSize),data(new int[other.maxSize]){
memcpy(data,other.data,sizeof(int)*maxSize);
}
//拷贝赋值
myqueue & operator = (const myqueue &other){
if(this==&other) return *this;
front=other.front;
rear=other.rear;
maxSize=other.maxSize;
int *temp=new int[maxSize];
delete [] data;
data=temp;
return *this;
}
int myfront(){
return data[front];
}
int back(){
if(empty()){
return -1;
}
return data[(rear-1+maxSize)%maxSize];
}
bool empty(){
return rear==front;
}
bool full(){
return (rear+1)%maxSize==front;
}
int size(){
return (front-rear+maxSize)%maxSize;
}
//入队
bool push(int val){
if(full()) return false;
data[rear]=val;
rear=(rear+1)%maxSize;
return true;
}
//出队
int pop(){
if(empty()) return -1;
int res=data[front];
front=(front+1)%maxSize;
return res;
}
void show(){
for(int i=front;i!=rear;i=(i+1)%maxSize){
cout<<data[i]<<" ";
}
cout<<endl;
}
};
int main(){
myqueue a(5);
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
a.pop();
a.push(6);
a.show();
}