derived.cpp

来自「C++课件,很好用的,帮助大家学习C++.」· C++ 代码 · 共 42 行

CPP
42
字号
// claa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Base {
public:	
	Base(int value=0):x(value) {}
	virtual ~Base() {}	
	Base(const Base& rhs):x(rhs.x) {}	
	Base& operator =(const Base& rhs){x=rhs.x; return *this;}
private:
	int x;
};

class Derived:public Base {
public:	Derived(int v):Base(v),y(v) { }	
		virtual ~Derived() {}	
		Derived(const Derived& rhs);	
		Derived& operator=(const Derived& rhs); 
private:	
	int y;
};
Derived& Derived::operator=(const Derived& rhs)
{
	if (this == &rhs) return *this;
	static_cast<Base&>(*this) = rhs; // 对*this的Base部分调用operator=
	y = rhs.y;
	return *this;
}
Derived::Derived(const Derived& rhs)
{
	*this =rhs;
}
	
int main(int argc, char* argv[])
{
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?