C + +: Object declaration

Source: Internet
Author: User


(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.