(i) Class-to-object relationships:
C + + calls classes of variables called Class objects, and objects are also called instances of classes
(ii) Definition of the object:
1. When declaring a class, directly define the object, that is, after declaring the right curly brace "}" of the class, write directly
The object name table that belongs to the class.
For example:
class point{
Public :
void SetPoint (int,int);
int getx ();
int gety ();
Private:
int x, y;
}op1,op2;
2. After declaring the class, define the object when it is used. Defining the format of an object and defining a basic data type variable
is similar in format to the following general format:
Class Name Object 1, Object 2, ..... ;
For example:
Point Op1,op2;
(iii) Access to members of the object:
The general form is:
The name of the object. Data member name or object name. member function name (argument table)
Examples are as follows:
#include <iostream>using namespacestd;classpoint{ Public: voidSetPoint (intAintb) {x=A; Y=b; } intGetx () {returnx; } intgety () {returny; } Private: intx, y; }OP1,OP2;intMain () {//Point op1,op2; inti,j; Op1.setpoint (1,2);//call the member function of the object OP1 SetPoint, assign a value to the OP1 data memberOp2.setpoint (3,4);//call the member function of the object OP2 SetPoint, assign a value to the OP1 data memberi = Op1.getx ();//call the object OP1 member function Getx, take the value of OP1 xj = op1.gety ();//call the object OP1 member function gety, take the value of Op1 ycout<<"OP1 i="<<i<<"OP1 j="<<j<<Endl; I= Op2.getx ();//call the object OP2 member function Getx, take the value of OP2 xj = op2.gety ();//call the object OP2 member function gety, take the value of Op2 ycout<<"OP2 i="<<i<<"OP2 j="<<j<<Endl; return 0;}
C + +: Object declaration