C++ classes and objects

Classes and Objects (Object Oriented)

  • C++ class definition

A class definition begins with the keyword class followed by the name of the class. The body of the class is enclosed in a pair of curly braces. The class definition must be followed by a semicolon . example:

class Box

{

  public:

     double length; // the length of the box

     double breadth; // the width of the box

     double height; // the height of the box

};

The keyword public identifies the access attributes of class members. In class object scope, public members are accessible outside the class. Members of a class can also be specified as private or protected.

Defining C++ Objects

Declare an object of a class just like declaring a variable of a primitive type. For example to declare two objects of class Box:

Box Box1; // Declare Box1, of type Box

Box Box2; // Declare Box2, of type Box

Objects Box1 and Box2 have their respective data members .

  • Access data members

The public data members of an object of a class can be accessed using the direct member access operator . E.g:

#include <iostream>

using namespace std;

class Box

{

  public:

     double length; // box length

     double breadth; // box width

     double height; // box height

};

int main( )

{

  Box Box1; // Declare Box1, of type Box

  Box Box2; // Declare Box2, of type Box

  double volume; // used to store the volume

  Box1.height = 5.0;

  Box1.length = 6.0;

  Box1.breadth = 7.0;

  Box2.height = 10.0;

   Box2.length =12.0;

  Box2.breadth = 13.0;

  volume = Box1.height * Box1.length * Box1.breadth; // the volume of box 1

  cout << "Volume of Box1:" << volume <<endl; // Volume of box 2

  volume = Box2.height * Box2.length * Box2.breadth;

  cout << "Box2 的体积:" << volume <<endl;

  return 0;

}

When the above code runs, it produces the following results:

Volume of Box1: 210

Volume of Box2: 1560

  •  class and object

1) Constructor and Destructor: The constructor of a class is a special function that is called when a new object is created. The destructor of a class is also a special function that is called when the created object is deleted. Destructors cannot be overloaded

2) This pointer in C++: Each object has a special pointer this, which points to the object itself.

3) Static members of C++ classes: Both data members and function members of a class can be declared static.

4) Class member functions: The member functions of a class are those functions whose definitions and prototypes are written inside the class definition, just like other variables in the class definition.

For example, the defined class Box uses member functions to access the members of the class, rather than directly accessing the members of these classes:

Member functions can be defined inside a class definition, or using :: . E.g:

class Box

{

  public:

     double length; // length

     double breadth; // width

     double height; // height

  

     double getVolume()

     {

        return length * breadth * height;

     }

};

or:

double Box::getVolume()

{

   return length * breadth * height;

}

 

#include <iostream>

using namespace std;

class Box

{

  public:

     double length; // the length of the box

     double breadth; // the width of the box

     double height; // height of the box

     // member function declaration

     double getVolume();

     void setLength(double l );

      void setBreadth( double b);

      void setHeight(double h);

};

// member function definition

double Box::getVolume(){return length *breadth* height;}

void Box::setLength(double l){length=l;}

void Box::setBreadth(double b){breadth=b;}

void Box::setHeight(double h){height=h;}

int main( )

{

  Box Box1; // Declare Box1, of type Box

  Box Box2; // Declare Box2, of type Box

  double volume;

   Box1.setLength(6.0);

  Box1.setBreadth(7.0);

  Box1.setHeight(5.0);

  Box2.setLength(12.0);

  Box2.setBreadth(13.0);

  Box2.setHeight(10.0);

  volume = Box1.getVolume();

  cout << "Box1 的体积:" << volume <<endl;

   volume= Box2.getVolume();

  cout << "Box2 的体积:" << volume <<endl;

  return 0;

}

When the above code runs, it produces the following results:

Volume of Box1: 210

Volume of Box2: 1560

  • Constructor with parameters

The default constructor does not have any parameters, the constructor can also take parameters if needed. This will assign an initial value to the object when it is created, such as:

#include <iostream>

using namespace std;

class Line

{

  public:

     void setLength( double len );

     double getLength( void );

     Line(double len);

  private:

     double length;

};

// member function definitions, including constructors

Line::Line( double len)

{

   cout << "length = " << len << endl;

length = len;

}

void Line::setLength( double len ){length =len;}

double Line::getLength(){return length;}

int main( )

{

  Line line(10.0);

   cout << "Length of line : "<< line.getLength() <<endl;

   line.setLength(6.0);

  cout << "Length of line : " << line.getLength()<<endl;

return 0;

}

When the above code runs, it produces the following results:

length = 10

Length of line : 10

Length of line : 6

  • copy constructor

A copy constructor is a special type of constructor that, when an object is created, initializes a newly created object with a previously created object in the same class. If there is no skin constructor defined in the class, the compiler will define one by itself. The copy constructor requires a reference parameter of class type.

classname::classname( const classname & referencename, ... ) ;

E.g:

class A

{ public:

   A(int);

  A(const A&,int=1);

   //…

};

//…

A a(1);

A b(a,0);

A c=b;

C++ this pointer

In C++, every object can access its own address through the this pointer. the this pointer is an implicit parameter of all member functions

#include <iostream>

using namespace std;

class Box

{

  public:

     // constructor definition

     Box(double l=2.0, double b=2.0, double h=2.0)

     {

        length = l;

        breadth = b;

        height = h;

     }

     double Volume()

     {

        return length * breadth * height;

     }

     int compare(Box box)

     {

        return this->Volume() > box.Volume();

     }

  private:

     double length;

double breadth;

     double height;

};

 

intmain()

{

    BoxBox1(3.3, 1.2, 1.5);

Box Box2(8.5, 6.0, 2.0);

  if(Box1.compare(Box2))

   {

     cout << "Box2 is smaller than Box1" <<endl;

   }

  else

   {

     cout << "Box2 is equal to or larger than Box1"<<endl;

   }

  return 0;

}

Box2 is equal to or larger than Box1

Static members of C++ classes

Use the static keyword to define class members as static. When a member of a class is declared static, it means that no matter how many objects of the class are created, there is only one copy of the static member .

Static static data members: encapsulation, inheritance, polymorphism.

A few points to note:

I. The basic framework of C++:

           define class

           main function (including the definition of the object: object.membername

           Objects can only be used through members and cannot be manipulated as a whole)

II. Objects cannot define globals

III. Access to class members is a class

          return value type classname::member function

          Member functions focus on operating on data members

IV. Member functions have two roles:

          1) Manipulate data members, including accessing and modifying data members

          2) Cooperate with different objects to operate (transfer information)

V. Access to object members includes:

1) Dot access form: object name. common members

2) Pointer

Experience:

       Class and object is a new concept ( object-oriented ), which contains many new concepts, which is different from what was learned some time ago ( process-oriented ). After learning classes and objects, we have the basic framework of C++ programs: define classes + main functions. Moreover, when writing systems with long and complicated codes, the content of classes and objects will be used to make the code appear more reasonable and orderly. It should be noted that due to the long and complex code, we need to define a class every time we write a program. To debug, write a class after debugging, and finally write a better program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706404&siteId=291194637