64,648
社区成员
发帖
与我相关
我的任务
分享
#include <IOSTREAM.H>
//基类
class CBase
{
int x;
public:
CBase(int n) {x=n;}
~CBase(){}
virtual void SetX(int n) {x=n;}
virtual int GetX() {return x;}
};
//派生类
class CDerive : public CBase
{
int x;
public:
CDerive(int n1,int n2):CBase(n1)
{
x=n2;
}
~CDerive() {}
void SetX(int n) {x=n;}
int GetX() {return x;}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
//基类
class CBase
{
int x;
public:
CBase(int n) {x=n;}
~CBase(){}
virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
virtual int GetX() {return x;}
};
//派生类
class CDerive : public CBase
{
int x;
public:
CDerive(int n1,int n2):CBase(n1)
{
x=n2;
}
~CDerive() {}
void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
int GetX() {return x;}
};
int main()
{
CBase *pb = new CDerive(1, 2);
pb->SetX(3);
pb->CBase::SetX(4);
}
#include <iostream>
using namespace std;
//基类
class CBase
{
int x;
public:
CBase(int n) {x=n;}
~CBase(){}
virtual void SetX(int n) {x=n;cout<<"CBase"<<endl;}
virtual int GetX() {return x;}
};
//派生类
class CDerive : public CBase
{
int x;
public:
CDerive(int n1,int n2):CBase(n1)
{
x=n2;
}
~CDerive() {}
void SetX(int n) {x=n;cout<<"CDerive"<<endl;}
int GetX() {return x;}
};
int main()
{
CBase *pb = new CDerive(1, 2);
pb->SetX(3);
pb->CBase::SetX(4);
}