Classes and objects for C + +

Source: Internet
Author: User

Object: This object, non-object,:-D, not with sister (but it seems to be possible,,), gossip Less, the book to the story

We can call all things we see as objects. It can be tangible, can be intangible, can be simple, can be complex, but for a specific object, such as the company.

Abstract:

We can abstract the attributes that all companies have, such as company name, address, industry, etc., these common attributes constitute a type-company class. Therefore, the understanding of the class in the computer, in the face of a specific problem (object), the first abstraction, to obtain the common nature of such specific problems, the general abstraction should include two parts: Data abstraction and Behavior abstraction, the former describes the object's attributes or state, the latter describes the common behavior or function.

Packaging:

Encapsulation is the combination of the resulting data and behavior to form an organism, which is the combination of the data and the function code of the manipulation data to form a "class" in which the data and functions are members of the class.

Inherited:

In order to solve the current problems, we will generally look at the previous knowledge of the problem and research results, and the use of these results. This is the inheritance, which has the ability to have in-depth understanding of the problem. That is, inherit the results of predecessors.

Polymorphic:

Polymorphism is the ability of a program to handle multiple types of objects, in C + + by forcing polymorphic, overloaded polymorphic, type-parameter polymorphism, including polymorphism.

Class:

The class is an abstract description of the problem, which encapsulates the related functions and data of the object. We can understand classes from another simple point of view, for example, basic data types, such as int,double, we declare a type variable

int i;

Double J;

Not only declare the variable i is used to store the data of the int class, but more importantly, restrict the operation of the variable, such as the only arithmetic and comparison of int type, which means that each data type includes the properties of the data itself and the operation of the data. Therefore, a class in C + + can be regarded as a user-defined data type, after defining a class, you can define a variable of that class, which is called the object (instance) of this class, and this process is called instantiation of the class.

The definition syntax for the class:

Class name

{

Public

external interface;

Protected

protected members;

Private

Private members;

};//Notice there is a semicolon yo!

Where public,protected,private represents the different access rights of the members respectively. Note that the prototype of a function can only be declared in a class, and the implementation of the function can be defined outside the class.

Example

Class Clock

{

Public

void settime (int newh,int newm,int News);

void Showtime ();

Protected

protected members;

Private

int hour,minute,scent;

}

Implementation of class functions

void clock::settime (int newh,int Newm,int News)

{

HOUR=NEWH;

MINUTE=NEWM;

Scend=news;

}

void Clock::showtime ()

{

cout<

}

Access rights for Class Members:

Public defines the outer interface of the class, and only the public members of the class can be accessed outside the class.

Private members are defined privately and can only be accessed by member functions of this class, and access to classes outside is illegal.

Protected defines a protection type member, similar to a private member, that differs in the impact of new classes produced during inheritance

member functions of the class

The behavior of the class when the member function of the class is the implementation part of the program algorithm, is the operation method of the encapsulated data.

Declaration and implementation of member functions

The prototype of a function is declared in a class, which describes the function's parameters and return value types, which are implemented outside the class. Unlike a normal function, a class implementation function needs to specify the name of the class, in the following form:

Return value type class Name:: function member name (parameter table)

{

function body

}

A member function of a class can also have a default parameter value (that is, the parameter can be assigned directly when the function is implemented)

Inline function: Don't write this first.

Object:

The object of a class in C + + is an entity (instance) of a class, and if the class is considered a custom type, the object of the class can be considered a variable of that type, in the following form:

Class name Object name;

When a class and its objects are defined, J can access the public members of the object, using the "." Form of access. operator, the general form is:

The name of the object. Public member function name (parameter table)//Note Do not forget the parameter table yo!

There are also constructors and destructors, where the function of the constructor is to initialize the object when it is created elsewhere, and the constructor is also a member function of the class, but the function name of the constructor is the same as the class name, and there is no return value, and the constructor is usually declared as a public function. Similar to the constructor, there is a copy constructor, this is not written, until later to learn the reference after writing.

On the destructor, is a clear function of the object, the destructor is the public function of the class, its name is the class name before the "~", there is no return value, do not accept any parameters, it is at the end of the lifetime of the object will be called automatically.

Finally, a complete class application is posted

There is a round pool that needs to be surrounded by a circular aisle and is surrounded by fences, fence price 35/m, aisle 20/m², road width three m, pool radius keypad input, aisle and fence cost

//example_1_2_class_swimmingpool.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;Const floatPi=3.1415926;Const floatFenceprice= *;Const floatConcreteprice= -;classCircle { Public: Circle (floatR);//constructors for initializing class values    floatcirsumference (); floatArea ();Private:    floatradius;};//Note that at the end of the class there is a semicolon, followed by curly braces, which is uncommon//implementation of the classCircle::circle (floatR) {radius=r;}//implementation of the constructor functionfloatCircle::cirsumference ()//calculate the perimeter of a circle{    return 2* PI *radius;}floatCircle::area ()//calculate the area of the garden{    returnpi*radius*radius;}int_tmain (intARGC, _tchar*argv[]) {    floatradius; floatFencost,concretecost; cout<<"Enter The radius of the pool:"; CIN>>radius;    Circle Pool (RADIUS); Circle Poolrim (Radius+3); Fencost=poolrim.cirsumference () *Fenceprice; cout<<"Fencing Cost is"<<fencost<<Endl; Concretecost= (Poolrim.area ()-pool.area ()) *Concreteprice; cout<<"Concrete Cost is"<<concretecost<<Endl; System ("Pause"); return 0;}

Classes and objects for C + +

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.