文章目录
前言
还是类和对象的学习,提高部分
一、this指针?
C++到C语言的翻译
class CCar { //对应结构体:struct CCar{
public: // int price;
int price; // };
void SetPrice(int p);
}; //成员函数会被翻译成全局的函数
void CCar::SetPrice(int p) //void SetPrice(struct CCar * this,int p)
{ price = p; } // { this -> price = p;}
int main() //int main(){
{ // struct CCar car;
CCar car; // SetPrice(& car,
car.SetPrice(20000); // 20000);
return 0; // return 0;
} // }
this指针的作用就是:
指向成员函数所作用的对象
非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针。
class Complex {
public:
double real, imag;
void Print() { cout << real << "," << imag ; }
Complex(double r,double i):real(r),imag(i)
{ }
Complex AddOne() {
this->real ++; //等价于 real ++;
this->Print(); //等价于 Print
return * this; //在这,返回它本身。
}
};
int main() {
Complex c1(1,1),c2(0,0);
c2 = c1.AddOne(); //给c2输出一个c1自增后的结果,输出2,1
return 0;
}
还可以用这个程序说明this指针的作用:
class A
{
int i;
public:
void Hello() { cout << "hello" << endl; }
};//void Hello(A * this ) { cout << this->i << "hello" << endl; }
//this若为NULL,则出错!!
int main()
{
A * p = NULL;
p->Hello(); //Hello(p);
} // 输出:hello
静态成员函数中不能使用 this 指针!
因为静态成员函数并不具体作用与某个对象!
因此,静态成员函数的真实的参数的个数,就是程序中写出的参数个数!
二、静态成员
1.基本概念
静态成员:在说明前面加了static关键字的成员。
class CRectangle
{
private:
int w, h;
static int nTotalArea; //静态成员变量
static int nTotalNumber;
public:
CRectangle(int w_,int h_);
~CRectangle();
static void PrintTotal(); //静态成员函数
};
本质差别:
普通成员变量每个对象有各自的一份,而静态成员变量一共就一份,为所有对象共享。
例如 :a1,a2都是对象,改了a1的static nTotalArea那么a2的这个变量也会改变
另外一个差别是sizeof运算符不会计算静态成员变量:
class CMyclass {
int n;
static int s;
};
则 sizeof( CMyclass ) 等于 4
静态成员函数:
普通成员变量每个对象有各自的一份,而静态成员变量一共就一份,为所有对象共享。
普通成员函数必须具体作用于某个对象,而静态成员函数并不具体作用于某个对象。
因此静态成员不需要通过对象就能访问。
2.如何访问静态成员?
- 类名::成员名
CRectangle::PrintTotal(); - 对象名.成员名
CRectangle r; r.PrintTotal(); - 指针->成员名
CRectangle * p = &r; p->PrintTotal(); - 引用.成员名
CRectangle & ref = r; int n = ref.nTotalNumber;
静态成员变量本质上是全局变量,哪怕一个对象都不存在,类的静态成员变量也存在。
静态成员函数本质上是全局函数。
设置静态成员这种机制的目的是将和某些类紧密相关的全局变量和函数写到类里面,看上去像一个整体,易于维护和理解。
栗子:
#include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;
class CRectangle
{
private:
int w, h;
static int nTotalArea;
static int nTotalNumber;
public:
CRectangle(int w_,int h_);
~CRectangle();
static void PrintTotal();
};
CRectangle::CRectangle(int w_,int h_)
{
w = w_;
h = h_;
nTotalNumber ++;
nTotalArea += w * h;
}
CRectangle::~CRectangle()
{
nTotalNumber --;
nTotalArea -= w * h;
}
void CRectangle::PrintTotal()
{
cout << nTotalNumber << "," << nTotalArea << endl;
}
int CRectangle::nTotalNumber = 0 ;
int CRectangle::nTotalArea = 0;
int main(){
CRectangle r1(3,3),r2(2,2);
//cout << CRectangle::nTotalNumber;
CRectangle::PrintTotal();
r1.PrintTotal();
r2.PrintTotal();
CRectangle r3(55,55);
r3.PrintTotal();
return 0;
}
输出结果:
注意!!!
在静态成员函数中,不能访问非静态成员变量,也不能调用非静态成员函数。
void CRectangle::PrintTotal()
{
cout << w << "," << nTotalNumber << "," <<
nTotalArea << endl; //wrong
}
CRetangle::PrintTotal(); //解释不通,w 到底是属于那个对象的?
CRectangle::CRectangle(int w_,int h_)
{
w = w_;
h = h_;
nTotalNumber ++;
nTotalArea += w * h;
}
CRectangle::~CRectangle()
{
nTotalNumber --;
nTotalArea -= w * h;
}
void CRectangle::PrintTotal()
{
cout << nTotalNumber << "," << nTotalArea << endl;
}
此CRectangle类写法,有何缺陷?
没有考虑到复制过构造函数。
在构造函数里nTotalNumber ++ ,析构函数里–。但是复制构造函数不会调用初始化构造函数。
在使用CRectangle类时,有时会调用复制构造函数生成临时的隐藏的CRectangle对象
- 调用一个以CRectangle类对象作为参数的函数时,
- 调用一个以CRectangle类对象作为返回值的函数时
临时对象在消亡时会调用析构函数,减少nTotalNumber 和nTotalArea的值,可是这些临时对象在生成时却没有增加nTotalNumber 和 nTotalArea的值。
解决方法
为CRectangle类写一个复制构造函数。
CRectangle :: CRectangle(CRectangle & r )
{
w = r.w; h = r.h;
nTotalNumber ++;
nTotalArea += w * h;
}
三、成员对象和封闭类
有成员对象的类叫 封闭(enclosing)类。
class CTyre //轮胎类
{
private:
int radius; //半径
int width; //宽度
public:
CTyre(int r,int w):radius(r),width(w) { } //轮胎类构造函数
};
class CEngine //引擎类,空的类
{
};
class CCar { //汽车类
private:
int price; //价格
CTyre tyre;
CEngine engine;
public:
CCar(int p,int tr,int tw );//构造函数
};
CCar::CCar(int p,int tr,int w):price(p),tyre(tr, w)
{
};
int main()
{
CCar car(20000,17,225);
return 0;
}
上例中,如果 CCar类不定义构造函数, 则下面的语句会编译出错:
CCar car;
因为编译器不明白 car.tyre该如何初始化。car.engine 的初始化没问题,用默认构造函数即可。
任何生成封闭类对象的语句,都要让编译器明白,对象中的成员对象,是如何初始化的。
具体的做法就是:通过封闭类的构造函数的初始化列表。
成员对象初始化列表中的参数可以是任意复杂的表达式,可以包括函数,变量,只要表达式中的函数或变量有定义就行。
封闭类构造函数和析构函数的执行顺序
·封闭类对象生成时,先执行所有对象成员的构造函数,然后才执行封闭类的构造函数。
·对象成员的构造函数调用次序和对象成员在类中的说明次序一致,与它们在成员初始化列表中出现的次序无关。
·当封闭类的对象消亡时,先执行封闭类的析构函数,然后再执行成员对象的析构函数。次序和构造函数的调用次序相反。
class CTyre {
public:
CTyre() { cout << "CTyre contructor" << endl; }
~CTyre() { cout << "CTyre destructor" << endl; }
};
class CEngine {
public:
CEngine() { cout << "CEngine contructor" << endl; }
~CEngine() { cout << "CEngine destructor" << endl; }
};
class CCar {
private:
CEngine engine;
CTyre tyre;
public:
CCar( ) { cout << “CCar contructor” << endl; }
~CCar() { cout << "CCar destructor" << endl; }
};
int main(){
CCar car;
return 0;
}
输出结果:
CEngine contructor
CTyre contructor
CCar contructor
CCar destructor
CTyre destructor
CEngine destructor
封闭类的对象,如果是用默认复制构造函数初始化的,那么它里面包含的成员对象,也会用复制构造函数初始化。
class A
{
public:
A() { cout << "default" << endl; }
A(A & a) { cout << "copy" << endl;}
};
class B { A a; };
int main()
{
B b1,b2(b1);
return 0;
}
输出:
default
Copy
说明b2.a是用类A的复制构造函数初始化的。而且调用复制构造函数时的实参就是b1.a。
四、常量对象、常量成员函数
1.常量对象和常量成员函数
如果不希望某个对象的值被改变,则定义该对象的时候可以在前面加 const关键字。
class Sample {
private :
int value;
public:
Sample() { }
void SetValue() { }
};
const Sample Obj; // 常量对象
Obj.SetValue (); //错误 。常量对象只能使用构造函数、析构函数和 有const 说明的函数(常量方法)
在类的成员函数说明后面可以加const关键字,则该成员函数成为常量成员函数。
常量成员函数内部不能改变属性的值,也不能调用非常量成员函数。
class Sample {
private :
int value;
public:
void func() { };
Sample() { }
void SetValue() const {
value = 0; // wrong
func(); //wrong
}
};
const Sample Obj;
Obj.SetValue (); //常量对象上可以使用常量成员函数
在定义常量成员函数和声明常量成员函数时都应该使用const 关键字。
class Sample {
private :
int value;
public:
void PrintValue() const;
};
void Sample::PrintValue() const { //此处不使用const会
//导致编译出错
cout << value;
}
void Print(const Sample & o) {
o.PrintValue(); //若 PrintValue非const则编译错
}
如果一个成员函数中没有调用非常量成员函数,也没有修改成员变量的值,那么,最好将其写成常量成员函数。
#include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;
class Sample
{
public:
int value;
void GetValue() const;
void func(){};
Sample(){}
};
void Sample::GetValue() const
{
value = 0;//wrong 常量成员函数是不允许修改 所作用的变量的值。
func();//wrong 不能调用非常量成员函数,因为非常量的其他函数有肯能修改到其他成员函数的值得
}
int main(){
const Sample o;
o.value = 100;//err.常量对象不可被修改
o.func();//err.常量对象上面不能执行非常亮成员函数
o.GetValue();//ok,常量对象上可以执行常量成员函数
return 0;
}//在Dev C++中,要为Sample类编写无参构造函数才可以,VS2010中不需要
2.常量成员函数的重载
两个函数,名字和参数表都一样,但是一个是const,一个不是,算重载
#include <iostream>
using namespace std;
class CTest {
private :
int n;
public:
CTest() { n = 1 ; }
int GetValue() const { return n ; }
int GetValue() { return 2 * n ; }
};
int main() {
const CTest objTest1;
CTest objTest2;
cout << objTest1.GetValue() << "," << objTest2.GetValue() ;
return 0;
}
=>1,2
可以在const成员函数中修改的成员变量
class CTest
{
public:
bool GetData() const
{
m_n1++;
return m_b2;
}
private:
mutable int m_n1;
bool m_b2;
};
3.常引用
对象作为函数的参数时,生成该参数需要调用复制构造函数,效率比较低。用指针做参数,代码不够好看,如何解决?
可以用对象的引用作为参数,如:
class Sample{
...
};
void PrintfObj(Sample & o)
{
...
}
这里好像前面学过了
担心引用会改变原本的值,使用常引用就不担心更改了。
五、友元(friends)
友元分为友元函数和友元类两种
- 友元函数: 一个类的友元函数可以访问该类的私有成员.
#include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;
class CCar ; //提前声明 CCar类,以便后面的CDriver类使用
class CDriver
{
public:
void ModifyCar( CCar * pCar) ; //改装汽车
};
class CCar
{
private:
int price;
friend int MostExpensiveCar( CCar cars[], int total); //声明友元
friend void CDriver::ModifyCar(CCar * pCar); //声明友元
};
void CDriver::ModifyCar( CCar * pCar)
{
pCar->price += 1000; //汽车改装后价值增加
}
int MostExpensiveCar( CCar cars[],int total)
//求最贵汽车的价格
{
int tmpMax = -1;
for( int i = 0;i < total; ++i )
if( cars[i].price > tmpMax)
tmpMax = cars[i].price;
return tmpMax;
}
int main()
{
return 0;
}
可以将一个类的成员函数(包括构造、析构函数)说明为另一个类的友元。
class B {
public:
void function();
};
class A {
friend void B::function();
};
- 友元类: 如果A是B的友元类,那么A的成员函数可以访问B的私有成员。
class CCar
{
private:
int price;
friend class CDriver; //声明CDriver为友元类
};
class CDriver
{
public:
CCar myCar;
void ModifyCar() {//改装汽车
myCar.price += 1000;//因CDriver是CCar的友元类,
//故此处可以访问其私有成员
}
};
int main(){ return 0; }
友元类之间的关系不能传递,不能继承。
江湖上说:你的朋友就是我的朋友
但这句话是不成立的,成不了朋友
总结
无