在普通的成员函数中,不管有没有定义形参
系统都会自动的加上一个隐藏的形参
该形参的类型为该类的指针类型-->(Score *)
该指针的作用:用来存放当前调用该成员函数对象的地址
一般将该指针称为this指针
Score::inScore(&s);
this->m_iNum;
特殊的成员函数
static
static 没有 this 指针
static 既可以通过类对象调用,也可以通过类名调用
static 函数中不能访问和操作普通的成员变量,
只能访问和操作静态的成员变量
一般将不需要访问和操作普通的成员变量的函数
但又和该类相关的函数申明定义为 static 成员函数
如果 static 成员变量在类外定义,则不需要加关键字 static
const
void show() const
有隐藏的 this 指针
由于成员函数声明为 const 成员函数,
该 this 指针会转变成为:const show *this
不能修改普通成员变量的值 -->const Show *this
但是可以修改静态成员变量 static 的值
一般将只访问不涉及修改成员变量的函数
申明为const成员函数,以防意外修改了成员变量
const成员函数只能被类对象调用
继承
多继承
虚继承(选择继承,多级继承可去重)
class Teacher: virtual public person
派生类会自动在类中添加一个指针
通过该指针来访问基类数据
由最派生类来真正继承被虚继承的类
被虚继承的类中的成员的初始化由最派生类来负责
#include <iostream>
using namespace std;
class Score
{
public:
//在普通的成员函数中,不管有没有定义形参
//系统都会自动的加上一个隐藏的形参
//该形参的类型为该类的指针类型--> (Score *)
//该指针的作用:用来存放当前调用该成员函数对象的地址
//一般将该指针称之为this指针
#if 0
void inScore(Score *this)
{
cin >> this->m_iNum >> this->m_fMath
>> this->m_fEnglish >> this->m_fProgramming;
this->m_fAvgScore = (this->m_fMath+this->m_fEnglish
+this->m_fProgramming)/3;
}
#endif
void inScore()
{
cin >> m_iNum >> m_fMath
>> m_fEnglish >> m_fProgramming;
m_fAvgScore = (m_fMath+m_fEnglish
+m_fProgramming)/3;
}
void showScore()
{
cout << "id:" << m_iNum
<< " score:" << m_fMath
<< ' ' << m_fEnglish
<< ' ' << m_fProgramming
<< " avgScore:" << m_fAvgScore << endl;
}
//1,static函数没有this指针
//2,static函数既可以通过类对象调用,
// 也可以通过类名调用
//3,static函数中不能访问和操作普通的成员变量
// 只能访问和操作静态的成员变量
//4,一般将不需要访问和操作普通的成员变量的函数
// 但又和该类相关的函数申明定义为static成员函数
//5,如果static成员变量在类外定义,则返回值类型前
// 不需要加static关键字
static void showHappy()
{
//cout << m_iNum << endl;
cout << m_iTestData << endl;
int iData = 90;
cout << "iData=" << iData << endl;
cout << "I am no.1, hahahahaha..." << endl;
}
static Score &getInstance();
private:
int m_iNum;
float m_fMath;
float m_fEnglish;
float m_fProgramming;
float m_fAvgScore;
static int m_iTestData;
};
Score& Score::getInstance()
{
static Score s;
return s;
}
int Score::m_iTestData = 19999;
int main(void)
{
Score s;
Score s2;
s.showHappy();
Score::showHappy();
Score &ref = Score::getInstance();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(){cout << "Person()...\n";}
Person(string id, string name
, int age, char sex, string addr)
{
m_strId = id;
m_strName = name;
m_iAge = age;
m_cSex = sex;
m_strAddress = addr;
cout << "Person(paragms..)...\n";
}
void info() const
{
cout << m_strId << ' ' << m_strName
<< ' ' << m_iAge << ' ' << m_cSex
<< ' ' << m_strAddress << endl;
}
private:
string m_strId;
string m_strName;
int m_iAge;
char m_cSex;
string m_strAddress;
};
//1,继承方式:public,protected,private
// 继承方式其实是对继承过来的成员进行封装
//2,若没有指定继承方式,则默认为private继承
// class Teacher: Person-->class Teacher: private Person
//3,只有一个继承,称之为单继承
//4,将被继承的类称之为基类
//5,新产成的类称之为派生类
//6,派生类需要对继承过来的数据和本身新增成员进行初始化
//7,对继承过来的成员初始化方式:
// 在派生类构造函数的初始化列表中,
// 显式或者隐式的调用基类的构造函数来对数据进行初始化
// 隐式的调用基类的构造函数:默认调用基类的无参构造函数
class Teacher: public Person
{
public:
#if 0
//以下两种情况:默认调用基类的无参构造函数
// 对继承过来的数据进行初始化
//即没有在派生类的初始化列表中写上基类的构造函数
//则默认调用基类的无参构造函数对继承过来的数据进行初始化
Teacher(){cout << "Teacher()...\n";}
Teacher(string courseName, float salary)
{
cout << "Teacher(paramgms..)...\n";
}
#endif
//Teacher(): Person()
Teacher(): Person("000", "newperson", 0, 'm', "china")
{
cout << "Teacher()...\n";
}
//Teacher(string courseName, float salary):Person()
Teacher(string id, string name
, int age, char sex, string addr
, string courseName, float salary)
: Person(id, name, age, sex, addr)
{
m_strCourseName = courseName;
m_fSalary = salary;
cout << "Teacher(paramgms..)...\n";
}
void info()
{
Person::info();
cout << m_strCourseName << ' ' << m_fSalary << endl;
}
private:
string m_strCourseName;
float m_fSalary;
};
int main(void)
{
#if 0
Person p("1001", "zhangsan", 18, 'f', "shanghai");
p.info();
#endif
//Teacher tec;
//Teacher tec2("chinese", 9000);
Teacher tec("1001", "xiaohua", 28, 'f', "nanhai"
, "english", 9000);
tec.info();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(){cout << "Person()...\n";}
Person(string id, string name
, int age, char sex, string addr)
{
m_strId = id;
m_strName = name;
m_iAge = age;
m_cSex = sex;
m_strAddress = addr;
m_strFace = "cool";
cout << "Person(paragms..)...\n";
}
void info() const
{
cout << m_strId << ' ' << m_strName
<< ' ' << m_iAge << ' ' << m_cSex
<< ' ' << m_strAddress << endl;
}
string m_strFace;
private:
string m_strId;
string m_strName;
int m_iAge;
char m_cSex;
string m_strAddress;
};
//1,继承方式:public,protected,private
// 继承方式其实是对继承过来的成员进行封装
//2,若没有指定继承方式,则默认为private继承
// class Teacher: Person-->class Teacher: private Person
//3,只有一个继承,称之为单继承
//4,将被继承的类称之为基类
//5,新产成的类称之为派生类
//6,派生类需要对继承过来的数据和本身新增成员进行初始化
//7,对继承过来的成员初始化方式:
// 在派生类构造函数的初始化列表中,
// 显式或者隐式的调用基类的构造函数来对数据进行初始化
// 隐式的调用基类的构造函数:默认调用基类的无参构造函数
//class Teacher: public Person
//class Teacher: protected Person
class Teacher: private Person
{
public:
#if 0
//以下两种情况:默认调用基类的无参构造函数
// 对继承过来的数据进行初始化
//即没有在派生类的初始化列表中写上基类的构造函数
//则默认调用基类的无参构造函数对继承过来的数据进行初始化
Teacher(){cout << "Teacher()...\n";}
Teacher(string courseName, float salary)
{
cout << "Teacher(paramgms..)...\n";
}
#endif
//Teacher(): Person()
Teacher(): Person("000", "newperson", 0, 'm', "china")
{
cout << "Teacher()...\n";
}
//Teacher(string courseName, float salary):Person()
Teacher(string id, string name
, int age, char sex, string addr
, string courseName, float salary)
: Person(id, name, age, sex, addr)
{
m_strCourseName = courseName;
m_fSalary = salary;
cout << "Teacher(paramgms..)...\n";
}
void info()
{
Person::info();
cout << m_strCourseName << ' ' << m_fSalary << endl;
cout << m_strFace << endl;
//cout << m_strName << endl;
}
private:
string m_strCourseName;
float m_fSalary;
};
//class Test:public Person;
int main(void)
{
Teacher tec("1001", "xiaohua", 28, 'f', "nanhai"
, "english", 9000);
tec.info();
// cout << tec.m_strFace << endl;
return 0;
}
//多继承
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(){cout << "Person()...\n";}
Person(string id, string name
, int age, char sex, string addr)
{
m_strId = id;
m_strName = name;
m_iAge = age;
m_cSex = sex;
m_strAddress = addr;
cout << "Person(paragms..)...\n";
}
void info() const
{
cout << m_strId << ' ' << m_strName
<< ' ' << m_iAge << ' ' << m_cSex
<< ' ' << m_strAddress << endl;
}
private:
string m_strId;
string m_strName;
int m_iAge;
char m_cSex;
string m_strAddress;
};
//虚继承,派生类会自动在类中添加一个指针
//通过该指针来访问基类数据
class Teacher: virtual public Person
{
public:
Teacher()
{
cout << "Teacher()...\n";
}
//Teacher(string courseName, float salary):Person()
Teacher(string id, string name
, int age, char sex, string addr
, string courseName, float salary)
: Person(id, name, age, sex, addr)
{
m_strCourseName = courseName;
m_fSalary = salary;
cout << "Teacher(paramgms..)...\n";
}
void info() const
{
Person::info();
cout << m_strCourseName << ' ' << m_fSalary << endl;
}
private:
string m_strCourseName;
float m_fSalary;
};
class Student: virtual public Person
{
public:
Student()
{
}
Student(string id, string name
, int age, char sex, string addr
, float math, float chinese
, float english, string grade)
: Person(id, name, age, sex, addr)
{
m_fMath = math;
m_fChinese = chinese;
m_fEnglish = english;
m_strGrade = grade;
cout << "Student(paragms..)...\n";
}
void info() const
{
Person::info();
cout << "score: " << m_fMath
<< ' ' << m_fChinese
<< ' ' << m_fEnglish
<< " 年级:" << m_strGrade << endl;
}
private:
float m_fMath;
float m_fChinese;
float m_fEnglish;
string m_strGrade;
};
//多继承情况时,基类构造函数的调用顺序和继承顺序保持一致
//和在初始化列表中的前后顺序无关
//在虚继承情况下,由最派生类来真正继承被虚继承的类
//被虚继承的类中的成员的初始化由最派生类负责
class GraduateStudent: public Teacher, public Student
{
public:
GraduateStudent(){}
GraduateStudent(string id, string name
, int age, char sex, string addr
, float math, float chinese
, float english, string grade
, string courseName, float salary
, string level, string project)
: Student(id, name, age, sex, addr
, math, chinese, english, grade)
, Teacher(id, name, age, sex, addr
, courseName, salary)
, Person(id, name, age, sex, addr)
{
m_strLevel = level;
m_strProject = project;
cout << "GraduateStudent(params...)\n";
}
void info() const
{
Student::info();
Teacher::info();
cout << m_strLevel << ' ' << m_strProject << endl;
}
private:
string m_strLevel;
string m_strProject;
};
//person由Test真正继承
class Test: public GraduateStudent
{};
int main(void)
{
GraduateStudent gs("1001", "xhangsan", 19
, 'f', "beijing", 90
, 98, 78, "二年级"
, "c++", 9000, "研究生"
, "机器自主学习");
gs.info();
cout << "person size:" << sizeof(Person) << endl;
cout << "student size:" << sizeof(Student) << endl;
cout << "teacher size:" << sizeof(Teacher) << endl;
cout << "graduate size:" << sizeof(GraduateStudent) << endl;
return 0;
}