C + + language class

Source: Internet
Author: User

in the real world, there are often objects belonging to the same class. For example, your bike is just one of many bicycles in the world. In object-oriented software, there are also many different objects that share the same characteristics: rectangles, employment records, video clips, and so on. You can use the same characteristics of these objects to create a collection for them. And this collection is called a class. A class is a blueprint or prototype that defines variables and methods for all objects of the same class. For example, you can create a bike class that defines instance variables, including the current gear. This class also defines and provides an implementation of instance methods (gearshift, brakes). The value of the instance variable is provided by each instance of the class. Therefore, when you create a bike class, you must instantiate it before you use it. When an instance of a class is created, an object of this type is established, and the system allocates memory for the instance variables defined by the class. You can then invoke an instance method of the object to implement some functionality. Instances of the same class share the same instance method. In addition to instance variables and methods, classes can also define class variables and class methods. Class variables and methods can be accessed from an instance of a class or directly from a class. Class methods can only manipulate class variables-no need to access instance variables or instance methods. The system creates a copy of all of its class variables for this class when it encounters a class for the first time-all instances of the class share its class variables. Excerpt from Baidu: Click to open the link

Next, take a look at the code, and give a simple case:

1.

#include <iostream>using namespace  std;//Definition Hotdog class//Note: Class does not have space class  hotdog{//Private member private:// Only the member method itself can be accessed or the friend function can access int age  ;    Member variable int  BBB;//Public member public://Member method  member function//static void Say_hello (void) static void Say_hello (void) {int A; int b; cout << "Hello hotdog" << Endl; }//General case a Get Set action method is required for a private member,  void set_age (int age) {this->age = age;} int get_age (void) {return this->age;} Declare a function as a friend function for the class friend  int main (void);//protected Member Protected:};int main (void) {class hotdog  Dog; Dog.age = 100; cout << "Age:" << dog.age << Endl; Dog.set_age,////cout << "Age:" << dog.get_age () << Endl; Unless the action method is declared static, you can call Hotdog::say_hello () directly with the class name; return 0;}

Execution Result: a:100

Hello Hotdog



C + + language class

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.