C + + Object Oriented Programming

C + + object oriented programming (3)

3, Class and object improvement

1.this pointer

C + + program to C program translation

class CCar{
  public:
  int price;
  void SetPrice(int p);
};
void CCar::SetPrice(int p){
  price=p;
}
int main(){
  CCar car;
  car.SetPrice(20000);
  return 0;
}
struct CCar{
  int price;
}
void SetPrice(struct CCar * this,int p){
  this->price=p;
}
int main(){
  struct CCar car;
  SetPrice(&car,20000);
  return 0;
}

Its function is to point to the object of the member object.

  • this pointer function

In a non static member function, you can directly use this pointer to represent the pointer to the object the function acts on.

class Complex{
  public:
  double real,imag;
  void Print(){cout<<real<<","<<imag;}
  Complex(double r,double i):real(r),imag(i){}
	Complex AddOne(){
    this->real++;
    this->Print();
    return * this;
  }
};
int main(){
  Complex c1(1,1),c2(0,0);
  c2=c1.AddOne();
  return 0;
}

Compare the following two procedures

class A{
  int i;
  public:void Hello(){
    cout<<"hello"<<endl;
  }
};
int main(){
  A*p=NULL;
  p->Hello();
}//Output hello
class A{
  int i;
  public:
  void Hello(){cout<<i<<"hello"<<endl;}
};
int main(){
  A *p=NULL;
  p->Hello();//Program error
}

this pointer cannot be used in static member function!

Because static member functions do not work specifically with an object!

Therefore, the number of real parameters of the static member function is the number of parameters written in the program!

Exercise:

The following statement is incorrect: (C)

A) this pointer cannot be used in static member functions

B) The this pointer is a pointer to the object on which the member function acts

C) this pointer is stored in the space of each object (error)

D) The non static member function of class has more real parameters than abbreviated parameters

2. Static members

Static member: a member whose description is preceded by the static keyword.

class CRectangle{
  private:
  int w,h;
  static int nTotalArea;//Static member variable
  static int nTotalNumber;
  public:
  CRectangle(int w_,int h_);
  ~CRectangle();
  static void PrintTotal();//Static member function
};

A common member variable has its own copy for each object, while a static member variable has a total of one copy, which is shared by all objects.

The sizeof operator does not evaluate static member variables.

class CMyclass{
  int n;
  static int s;
};
//sizeof(CMyclass) is equal to 4

A common member variable has its own copy for each object, while a static member variable has a total of one copy, which is shared by all objects.

Ordinary member functions must act specifically on an object, while static member functions do not act specifically on an object.

Therefore, static members do not need to be accessed through objects.

  • How to access static members

(1) Class name:: member name

​ CRectangle::PrintTotal();

(2) Object name. Member name

​ CRectangle r;r.PrintTotal();

(3) Pointer - > member name

​ CRectangle *p=&r;r->PrintTotal();

(4) Reference. Member name

​ CRectangle & ref=r;int n=ref.nTotalNumber;

Static member variables are essentially global variables. Even if an object does not exist, the static member variables of the class also exist.

Static member functions are essentially global functions.

The purpose of this mechanism is to write global variables and functions closely related to some classes into the class. It looks like a whole and is easy to maintain and understand.

  • Static member example

Consider a graphics processing program that needs to know the total number and total area of rectangles at any time

You can use global variables to record the total number and total area

Encapsulating these two variables into a class with static members makes it easier to understand and maintain

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;
//Static member variables must be described once in the file that defines the class
//Or initialization, otherwise the compilation can pass and the link cannot pass
int main(){
  CRectangle r1(3,3),r2(2,2);
  //cout<<CRectangle::nTotalNumber;// Wrong, private
  CRectangle::PrintTotal();
  r1.PrintTotal();
  return 0;
}

In a static member function, you cannot access non static member variables or call non static member functions.

-The above description of the krectangle class is defective

When using the crictangle class, the copy constructor is sometimes called to generate a temporarily hidden crictangle object

(when calling a function with the crictangle class object as the parameter, when calling a function with the crictangle class object as the return value)

When temporary objects die, they call destructors to reduce the values of nTotalNumber and nTotalArea, but these temporary objects do not increase the values of nTotal and nTotalArea when they are generated.

Solution: write a copy constructor for the krectangle class

CRectangle::CRectangle(CRectangle &r){
  w=r.w;h=r.h;
  nTotalNumber++;
  nTotalArea+=w*h;
}

Exercise:

Which of the following is incorrect? (B)

A) Static member functions cannot access non static member variables of the same kind or call non static member functions of the same kind.

B) Non static member function cannot access static member variable (error)

C) Static member variables are shared by all objects

D) Static members of a class can also be accessed without any objects

Tags: C++

Posted on Sun, 21 Nov 2021 05:04:57 -0500 by mingo