C + + 's lesson object-oriented (i)

Source: Internet
Author: User

What is a class and what is an object? How do I define a class? How do I define an object?

First, to define a human

  

class person{  public:      void  getweight ();       void   setweight ();   Private:    int  weight;      };

Declare a human with the class keyword. Now just declaring a class without using a class? The system does not allocate memory to the class. Public is a common member method. Private is a privately owned data member. When declaring a class, do not forget the (;) number.

Then we can define an object for a human such as:

Person Jack;

What is the difference between a class and an object?

A class is abstract, and an object is an instance of a class.

Now that we know the difference between a class and an object, how do you access the members and methods of the object? We can use (.) To access

such as Jack.wight; Access members

You can also assign a value to a member Jack.wight = 160;

such as Jack.getweight (); Access method

Instance explaining the data members and methods in the calling class

 

#include <iostream>using namespacestd;//---------------------------------------------------------------------------classperson{ Public:    voidSetweight (intm_weight); voidgetweight ();Private:    intWeight;};voidPerson::setweight (intm_weight) {Weight=m_weight;}voidperson::getweight () {cout<<Weight<<Endl;}int_tmain (intARGC, _tchar*argv[])    {Person Jack; Jack.setweight ( the);    Jack.getweight (); return 0;}//---------------------------------------------------------------------------

  

The above is the most basic class of calls.

  

 

C + + 's lesson object-oriented (i)

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.