"C + + object-oriented Learning Notes"-Basic knowledge __c++

Source: Internet
Author: User
Tags class definition variable scope
1. Basic Concepts

Object-oriented programming is the use of objects to program programming. An object represents a unique entity in the real world. For example, a student, a table, a circle, etc. can be considered as objects. An object has a unique identity, state, and behavior. The state of an object is represented by the data field and their current values. The behavior of an object is defined by a set of functions, and a function called on an object is a request to perform a task.

Objects of the same type are defined with a generic class. A class is a template or a blueprint that defines what data and functions an object has. An object is an instance of a class, and we can create multiple objects of a class. The creation of an instance is called instantiation. The newly created object is saved in a memory area, and when the object is created, we can use the dot operator (.), the so-called object member access operator, to access the object's data and invoke the object's function. Here is an example:

Class Circle
{public
:
	double radius;
	Circle ();
	{
		radius=1;
	}
	Circle (double Newradius)
	{
		radius=newradius;
	}
	Double Getarea ()
	{return
		radius*radius*3.14159;
	}
};
int main ()
{
	Circle circle1;
	Circle Circle2 (5.0);
	cout<< "The" of the "Circle of Radius" <<circle1.radius<< "is" <<circle1.getarea () << Endl;
	cout<< "The" of the "Circle of Radius" <<circle2.radius<< "is" <<circle2.getarea () << Endl;
	circle2.radius=100;
	cout<< "The" of the "Circle of Radius" <<circle2.radius<< "is" <<circle2.getarea () << Endl;
	return 0;
}

The results of the program output are as follows:

The area of the circle of radius 1 is 3.14159

The area of the circle of radius 5 is 78.5397

The area of the circle's radius is 31415.9

Note the point:

1 Be sure to have a semicolon at the end of the class definition (;), union and struct.)

2 A class member whose data field cannot be initialized at declaration time and can be assigned a value in the constructor. 2. Declaration and implementation Separation

C + + allows the declaration and implementation of a class to be detached. Class declarations describe the conventions of a class, and the class implementation implements this Convention. Class declarations simply list all data fields, constructor prototypes, and function prototypes, and the class implementation gives the implementation of constructors and member functions, which are placed in two separate files. Two files should use the same name, but have different extensions. The class declaration file has an. h extension, and the class implements the. cpp extension. The above example is now declared and implemented in isolation.

Program List Circle.h

Class Circle
{public
:
	double radius;
	Circle ();
	Circle (double newradius);
	Double Getarea ();
};

Program List Circle.cpp

#include "Circle.h"
circle::circle ()
{
	radius=1;
}
Circle::circle (double Newradius)
{
	radius=newradius;
}
Double Circle::getarea ()
{return
	radius*radius*3.14159;
}

Note the point:

1 before each constructor and function circle:: Is required, it tells the compiler that these functions are defined in the Circle class. In C + +, the symbol:: Called the two-yuan scope resolution operator.

2 If a function is implemented within a class declaration, it automatically becomes an inline function. There is another way to declare a class member function inline, using inline in the class implementation file to indicate that the member function is an inline function. 3. Object pointer and array of objects

1) Object pointer

Once the object name is declared, it cannot be modified. However, we can create object pointers, and whenever necessary, we can assign the object address to the pointer. For example:

Circle Circle1;

Circle *pcircle=&circle1;

There are two ways to access object members through pointers. First, dereference the pointer, and then use the dot operator (.) Access to object members. Second, C + + provides a shorthand member selection operator, the arrow operator (->). The following are examples of implementing pointers to access object members in both of these ways:

cout<< "The radius is" << (*pcircle) .radius<<endl;

cout<< "The" is "<<pcircle->getarea () <<endl;

2) Object array

We can create an array of elements as basic data types, and we can also create arrays of objects. For example:

Circle CIRCLEARRAY[10];

This statement invokes the parameterless constructor to initialize the elements in the array.

NOTE: When you declare an object pointer, you simply declare a pointer to an object and do not call the constructor, and when you declare an object array, you call the constructor to initialize the object element. 4. Variable Scope

In a class, you can declare only one member variable for one data field, but a variable name can be used to declare multiple local variables in several different functions. Local variables are declared within a function and can only be used locally within a function. If a local variable has the same name as a data field (in a member function), the data field is masked because the local variable has a higher precedence.



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.