#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People(char*, int);
};
People::People(char *name, int age): name(name), age(age){}
//派生类
class Student: public People{
private:
float score;
public:
Student(char*, int, float);
void display();
};
//调用了基类的构造函数
Student::Student(char *name, int age, float score): People(name, age){
this->score = score;
}
void Student::display(){
cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}
int main(){
Student stu("小明", 16, 90.5);
stu.display();
return 0;
}
Student::Student(char *name, int age, float score): People(name, age)
Student::Student(char *name, int age, float score): People(name, age), score(score){}
Student::Student(char *name, int age, float score): People("李磊", 20)
#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People();
People(char*, int);
};
People::People(){
this->name = "xxx";
this->age = 0;
}
People::People(char *name, int age): name(name), age(age){}
//派生类
class Student: public People{
private:
float score;
public:
Student();
Student(char*, int, float);
void display();
};
Student::Student(){
this->score = 0.0;
}
Student::Student(char *name, int age, float score): People(name, age){
this->score = score;
}
void Student::display(){
cout<<name<<"的年龄是"<<age<<",成绩是"<<score<<endl;
}
int main(){
Student stu1;
stu1.display();
Student stu2("小明", 16, 90.5);
stu2.display();
return 0;
}
xxx的年龄是0,成绩是0 小明的年龄是16,成绩是90.5
#include<iostream>
using namespace std;
//基类
class People{
protected:
char *name;
int age;
public:
People();
People(char*, int);
};
People::People(): name("xxx"), age(0){
cout<<"PeoPle::People()"<<endl;
}
People::People(char *name, int age): name(name), age(age){
cout<<"PeoPle::People(char *, int)"<<endl;
}
//派生类
class Student: public People{
private:
float score;
public:
Student();
Student(char*, int, float);
};
Student::Student(): score(0.0){
cout<<"Student::Student()"<<endl;
}
Student::Student(char *name, int age, float score): People(name, age), score(score){
cout<<"Student::Student(char*, int, float)"<<endl;
}
int main(){
Student stu1;
cout<<"--------------------"<<endl;
Student stu2("小明", 16, 90.5);
return 0;
}
PeoPle::People() Student::Student() -------------------- PeoPle::People(char *, int) Student::Student(char*, int, float)
Student s1; //Student是已声明的类名,s1是Student类的对象
#include <iostream>
#include <string>
using namespace std;
class Student//声明基类
{
public: //公用部分
Student(int n, string nam ) //基类构造函数,与例11.5相同
{
num=n;
name=nam;
}
void display( ) //成员函数,输出基类数据成员
{
cout<<"num:"<<num<<endl<<"name:"<<name<<endl;
}
protected: //保护部分
int num;
string name;
};
class Student1: public Student //声明公用派生类Student1
{
public:
Student1(int n, string nam,int n1, string nam1,int a, string ad):Student(n,nam),monitor(n1,nam1) //派生类构造函数
{
age=a;
addr=ad;
}
void show( )
{
cout<<"This student is:"<<endl;
display(); //输出num和name
cout<<"age: "<<age<<endl; //输出age
cout<<"address: "<<addr<<endl<<endl; //输出addr
}
void show_monitor( ) //成员函数,输出子对象
{
cout<<endl<<"Class monitor is:"<<endl;
monitor.display( ); //调用基类成员函数
}
private: //派生类的私有数据
Student monitor; //定义子对象(班长)
int age;
string addr;
};
int main( )
{
Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai");
stud1.show( ); //输出学生的数据
stud1.show_monitor(); //输出子对象的数据
return 0;
}
This student is: num: 10010 name: Wang-li age: 19 address:115 Beijing Road,Shanghai Class monitor is: num:10001 name:Li-sun
Student monitor; //定义子对象 monitor(班长)
Student1(int n, string nam,int n1, string nam1,int a, string ad):
Student(n,nam),monitor(n1,nam1)
Student1(int n, string nam,int n1, string nam1,int a, string ad): monitor(n1,nam1),Student(n,nam)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有