2012年(1008)
分类:
2012-08-01 11:08:54
原文地址:单继承 作者:luozhiyong131
/*
* 继承
* 从基类CPoint(点)公有派生类CCircle(圆)、CClinder(圆柱体)的声明
* 简单展示继承的优点
* Lzy 2011-8-5
*/
#include
using namespace std;
class CPoint
{
protected:
float x, y;
public:
CPoint(float _x, float _y):x(_x),y(_y){}
float getX(){return x;}
float getY(){return y;}
};
class CCircle : public CPoint
{
protected:
float r;
public:
CCircle(float _r):r(_r){}
double area(){return 3.14*r*r;}
double circumference(){return 3.14*2*r;}
};
class CClinder:public CCircle
{
protected:
float h;
public:
CClinder(int _h):h(_h){}
float volume(){return 3.14*r*r*h;}
};
/*
* 公有继承
* 从基类学生公有派生研究生类
* Lzy 2011-8-5
*/
#include
#include
using namespace std;
class Student
{
private:
char *name;
int No;
public:
Student(char *nstr="", int n=0)
{
name = new char[strlen(nstr)+1];
strcpy(name, nstr);
No = n;
}
~Student(){delete[] name;}
char * getname(){return name;}
int getNo(){return No;}
};
class GraduateStudent : public Student
{
private:
char *instructor;
public:
GraduateStudent(char *nstr="", int n=0, char *dstr=""):Student(nstr,n)
{
instructor = new char[strlen(dstr)+1];
strcpy(instructor, dstr);
}
~GraduateStudent(){delete[] instructor;}
char *getInstructor(){return instructor;}
};
int main(void)
{
GraduateStudent lzy("罗志勇", 1001,"Lzy");
cout<<"姓名:"<
<<"学号:"<
<<"导师:"<
cout<
return 0;
}
/*
* 私有继承
* Lzy 2011-8-5
*/
#include
using namespace std;
class A
{
private:
int a;
public:
A():a(1){}
int getA(){return a;}
};
class B : private A
{
private:
int b;
public:
void setB(){b=getA()+10;}
void print()
{
A::print();
cout<<"B b="<
}
};
int main(void)
{
A a;
a.print();
B b;
b.setB();
b.print();
return 0;
}
/*
* 私有继承
* Lzy 2011-8-5
*/
#include
using namespace std;
class A
{
private:
int a;
public:
A():a(1){}
int getA(){return a;}
};
class B : private A
{
private:
int b;
public:
void setB(){b=getA()+10;}
void print()
{
A::print();
cout<<"B b="<
}
};
int main(void)
{
A a;
a.print();
B b;
b.setB();
b.print();
return 0;
}
/*
* 保护继承
* 从基类学生公有派生研究生类
* Lzy 2011-8-5
*/
#include
#include
using namespace std;
class Student
{
private:
char *name;
int No;
public:
Student(char *nstr="", int n=0)
{
name = new char[strlen(nstr)+1];
strcpy(name, nstr);
No = n;
}
~Student(){delete[] name;}
char * getname(){return name;}
int getNo(){return No;}
};
class GraduateStudent : protected Student
{
private:
char *instructor;
public:
GraduateStudent(char *nstr="", int n=0, char *dstr=""):Student(nstr,n)
{
instructor = new char[strlen(dstr)+1];
strcpy(instructor, dstr);
}
~GraduateStudent(){delete[] instructor;}
char *getInstructor(){return instructor;}
int getNo(){return Student::getNo();}
char *getname(){return Student::getname();}
};
int main(void)
{
GraduateStudent lzy("罗志勇", 1001,"Lzy");
cout<<"姓名:"<
<<"学号:"<
<<"导师:"<
cout<
return 0;
}
/*
* 继承
* 类中包含子对象的构造函数的执行情况
* Lzy 2011-8-5
*/
#include
#include
using namespace std;
class CPoint
{
private:
float x, y;
public:
CPoint(float _x, float _y):x(_x),y(_y){cout<<"构造函数CPoint被调用:"<
CPoint(CPoint & p):x(p.x),y(p.y){cout<<"复制构造函数CPoint被调用:"<
void display(){cout<
};
class CCline
{
private:
CPoint start, end;
public:
CCline(float x1, float y1, float x2, float y2):start(x1,y1),end(x2,y2)
{cout<<"构造函数CCline被调用";start.display();end.display();}
CCline(CPoint &p1, CPoint &p2):start(p1),end(p2)
{cout<<"复制构造函数CCline被调用";start.display();end.display();}
};
int main(void)
{
CPoint p1(12,15),p2(25,35);
CCline c1(1,1,2,2),c2(p1,p2);
return 0;
}