成员函数、继承(20170213)
2017-02-13 本文已影响13人
Hassan_chao
C++06
区分new/delete malloc/free
new /delete 运算符,不需要转换,自动调用析构函数
malloc /free 函数,返回值转换,释放空间
成员函数
在普通成员函数中,不管是否定义形参,系统都会自动加上一个隐藏的形参,该形参类型为该类的指针类型,即( Score *)。该指针的作用,用来存放当前调用该成员函数对象的地址,一般将该指针称之为this指针。
static成员函数
stastic 函数没有this 指针
static函数 既可以通过类对象调用,也可以通过类名调用
static 函数中,不同访问和操作普通成员变量,只能访问和操作静态成员变量
一般将不需要访问和操作普通成员变量函数,但又和该类相关的函数声明定义为static函数,如果stastic成员变量,在类外定义,则返回值类型前不需要加stastic
#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;
}
const成员函数
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
public:
Point(float x = 0.0f, float y = 0.0f)
{
m_x = x;
m_y = y;
}
//getX(Point *this)
float getX()
{
return m_x;
}
float getY()
{
return m_y;
}
//1,const成员函数有隐藏的this指针
//2,由于成员函数申明const成员函数
// 该this指针会转变为: const Point *this
//3,不能修改普通成员变量的值-->const Point *this
// 但是可以修改static成员变量的值
//4,一般将只访问不涉及修改成员变量的函数
// 申明为const成员函数,以防意外修改了成员变量
//5,const成员函数之只能被类对象调用
void show() const
{
cout << '(' << m_x << ',' << m_y << ')' << endl;
//this->m_x = 90; //error
m_iTestData = 999;
cout << "m_iTestData=" << m_iTestData << endl;
}
private:
float m_x;
float m_y;
static int m_iTestData;
};
int Point::m_iTestData = 90;
int main(void)
{
Point p1(2, 3);
Point p2(5, 7);
p1.show(); //-->Point::show(&p1);
p2.show();
//const point -> common point //errror
//common point -> const point //right
//const int a = 90;
const Point p3(12, 34);
p3.getX();// -->Point::getX(&p3);
return 0;
}
C++的继承
单继承
多继承
虚继承
class Teacher:virtual public Presion
派生类,会自动在类中添加一个指针,通过指针访问基类数据
在虚继承情况下,由最派生类真正继承被虚继承的类
被虚继承的类中的成员的初始化,由最派生类负责初始化
//单继承
#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: 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: 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)
{
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;
};
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;
}
//虚拟继承
#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;
}