#include
using namespace std;
class A
{
public:
int x;
int y;
public:
void set(int x1,int y1)
{
x=x1;
y=y1;
}
virtual void print()
{
cout<<"A中的print()"< }
};
class B:public A
{
private:
int z;
void set()
{
}
void print()
{
cout<<"B中的print()"< }
void printB()
{
cout<<"B中的printB()"<
}
};
class C:public A
{
private:
int z;
void set()
{
}
void print()
{
cout<<"C中的print()"< }
};
int main()
{
A *p;
B b;
C c;
p=&b;
p->print();
p=&c;
p->print();
cout<<"虚函数表地址="<<*((void**)&b)</* A a;
cout<<"sizeof(a)="<
cout<<"sizeof(A)="<
cout<<"&a="<<&a<
cout<<"&a.x="<<&a.x< return 0;
}
******************************************************************************
#include
using namespace std;
class tx
{
protected:
double s;
double l;
public:
virtual void print()=0;
};
class yuan:public tx
{
private:
double pi;
double r;
public:
yuan(double r1):pi(3.14)
{
r=r1;
}
void print()
{
s=pi*r*r;
cout<<"r="<
cout<<"s="<
cout<<"r="< }
};
int main()
{
yuan yuan1(10);
tx *p;
p=&yuan1;
p->print();
return 0;
}
************************************************************************************
#include
using namespace std;
class tx
{
protected:
double s;
double l;
public:
virtual void Aa(int x=100)
{
cout<<"x="<
cout<<"Tx"< }
virtual void js()
{
}
virtual void print()
{
}
virtual ~tx()
{
cout<<"tx析构"< }
};
class yuan:public tx
{
private:
const double pi;
double r;
public:
virtual void Aa(int x=200)
{
cout<<"x="<
cout<<"yuan"< }
yuan():pi(3.14)
{
r=0;
}
yuan(double r1):pi(3.14)
{
if(r1>=0)
{
r=r1;
}
else
{
r=0;
cout<<"半径不能为负数,初始化错误"<
}
}
void js()
{
s=pi*r*r;
l=2*pi*r;
}
void print()
{
cout<<"s="<
cout<<"l="<
}
~yuan()
{
cout<<"yuan析构"< }
};
class cfx:public tx
{
private:
double a;
double b;
public:
cfx()
{
a=0;
b=0;
}
cfx(double a1,double b1)
{
if(a1>=0 && b1>=0)
{
a=a1;
b=b1;
}
else
{
a=0;
b=0;
cout<<"初始化错误"<
}
}
void js()
{
s=a*b;
l=2*(a+b);
}
void print()
{
cout<<"s="< cout<<"l="< }
};
int main()
{
tx *p=new yuan;
delete p;
/* tx *p;
int x=0;
while(1)
{
cout<<"请输入计算图形类型"<
cout<<"(1)圆"<
cout<<"(2)长方形"<
cout<<"(3)退出"< cin>>x;
if(x==1)
{
double r;
cout<<"请输入圆半径"< cin>>r;
p=new yuan(r);
}
if(x==2)
{
double a,b;
cout<<"请输入长方形长"< cin>>a;
cout<<"请输入长方形宽"< cin>>b;
p=new cfx(a,b);
}
if(x==3)
{
delete p;
p=NULL;
break;
}
p->js();
p->print();
delete p;
p=NULL;
}*/
return 0;
}
阅读(1577) | 评论(0) | 转发(1) |