派生类定义(学生类)
#include <iostream>
#include <string>
using namespace std;
class Student//声明基类
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
protected:
int num;
string name;
char sex;
};
class Student1:public Student//声明派生类
{
public:
void get_value1()
{
get_value();
cin>>age>>add;
}
void display1()
{
display();
cout<<"age:"<<age<<endl;
cout<<"add:"<<add<<endl;
}
private:
int age;
string add;
};
int main()
{
Student1 s;
s.get_value1();
s.display1();
return 0;
}
派生类编程(点类——>圆类)
#include <iostream>
#include <string>
using namespace std;
class Point//声明基类
{
public:
void get_value()
{
cin>>x>>y;
}
void display()
{
cout<<x<<endl;
cout<<y<<endl;
}
protected:
float x,y;
};
class Circle:public Point//声明派生类
{
public:
void get_value1()
{
get_value();
cin>>r;
}
void display1()
{
display();
cout<<r<<endl;
}
float area()
{
float m;
m=3.14*r*r;
return m;
}
private:
float r;
};
int main()
{
Circle s;
s.get_value1();
s.display1();
cout<<s.area();
return 0;
}
派生类(点–圆--圆柱体类)
#include <iostream>
using namespace std;
class Point//声明基类
{
public:
void get()
{
cin>>x>>y;
}
void display()
{
cout<<x<<","<<y<<endl;
}
protected:
float x,y;
};
class Circle:public Point//声明派生类
{
protected:
float r;
public:
void get1()
{
get();
cin>>r;
}
void display1()
{
display();
cout<<"r="<<r<<endl;
}
float area()
{
float m;
m=3.14*r*r;
return m;
}
float perimeter()
{
float l;
l=2*3.14*r;
return l;
}
};
class Cylinder:public Circle
{
private:
float h;
public:
void get2()
{
get1();
cin>>h;
}
void display2()
{
display1();
cout<<"h="<<h<<endl;
}
float volume()
{
float v;
v=h*area();
return v;
}
};
int main()
{
Cylinder cy;
cy.get2();
cy.display2();
cout<<cy.volume()<<endl;
return 0;
}