C ++ self-study notes

Source: Internet
Author: User

C ++ self-study notes
Abstract Some objects with the same attributes and operations into a class. These objects are instances of this class. Encapsulation is an Information Hiding Technology. encapsulation can hide some attributes and operations, so that users are not allowed to use them. The other part serves as the external interface of the class. Inheritance is to create a new class to obtain the existing features from the existing class. Inheritance effectively realizes software reuse and enhances the scalability of the system. Polymorphism is an important mechanism for object-oriented programming. A class has more powerful functions than a basic type. A class is the encapsulation body of data and functions;
The member function describes the behavior of a class for operations on data members in the class. Because the object's encapsulation class member function is the only way to operate on private data members of the class.
For program reliability, data members are generally hidden.
# Include <iostream. h>


Void meet (double x, double y)
{
Cout <"2 sum is ";
Cout <x + y <endl;
}
Void meet (char x)
{
Cout <"string :";
Cout <x <endl;
}
Int main ()
{
Meet (7.2, 5.3 );
Meet ('A ');
Return 0;
}
A class can have no private members, but cannot have public members. Therefore, when designing a class, you must have a public member, which is an external interface of the class. To achieve information hiding, you must have a private member.
# Include <iostream>


Using namespace std;
Class Date {
Private:
Int year;
Int month;
Int day;
Public:
Void Set (int a, int B, int c)
{
Year =;
Month = B;
Day = c;
}
Void Display ()
{
Cout <"the current time is" <year <"year" <month <"month" <day <"day" <endl;
}
};
Class shijian {
Private:
Int hour, minute, second;
Public:
Void Set (int a, int B, int c)
{
Hour =;
Minute = B;
Second = c;
}
Void Display ()
{
Cout <"current time is" }
};
Int main () // main Function
{
Shijian a; // the object of the class can call the Public Functions of the class.
A. Set (, 17 );
A. Display ();
Return 0;
}


The implementation of member functions can be done in this way, and the scope can be added out of the class:
# Include <iostream>


Using namespace std;
Class Date {
Private:
Int year;
Int month;
Int day;
Public:
Void Set (int a, int B, int c); // member function
Void Display (); // member function
};
Class shijian {
Private:
Int hour, minute, second;
Public:
Void Set (int a, int B, int c)
{
Hour =;
Minute = B;
Second = c;
}
Void Display ()
{
Cout <"current time is" }
};
Void Date: Set (int a, int B, int c)
{
Year =;
Month = B;
Day = c;
}
Void Date: Display ()
{
Cout <"Date:" <endl;
Cout <"\ t" <year <"year ";
Cout <month <"month ";
Cout <day <"day" <endl;
}
Int main () // main Function
{
Date a; // the object of the class can call the Public Functions of the class.
A. Set (, 17 );
A. Display ();
Return 0;
}


Design a class containing four integers. the maximum and minimum values of these four numbers must be obtained.


# Include <iostream. h>


Class MaxMin4 {
Private:
Int a, B, c, d;
Int Max2 (int, int );
Int Min2 (int, int );
Public:
Void Set (int, int );
Int Max4 ();
Int Min4 ();
};
Int MaxMin4: Max2 (int x, int y)
{
If (x> y)
Return x;
Else
Return y;
}
Int MaxMin4: Min2 (int x, int y)
{
If (x <y)
Return x;
Else
Return y;
}
Int MaxMin4: Max4 ()
{
Int m, n;
M = Max2 (a, B );
N = Max2 (c, d );
Return Max2 (m, n );
}
Int MaxMin4: Min4 () // because this is the minimum value in your class
{
Int m, n;
M = Min2 (a, B );
N = Min2 (c, d );
Return Min2 (m, n );


}
Void MaxMin4: Set (int x1, int x2, int x3, int x4)
{
A = x1;
B = x2;
C = x3;
D = x4;
}
Int main ()
{
MaxMin4 maxmin4;
Maxmin4.Set (8, 6, 5, 9 );
Cout <"maximum output of the four numbers" <maxmin4.Max4 () <endl;
Cout <"output the minimum values of the four numbers" <maxmin4.Min4 () <endl;
Return 0;
}
Compile a computer and implement it in c ++
# Include <iostream. h> // The fate is arranged by myself. No one can take the lead.


Class JiSuanQi {
Private: int A, B, C;
Public:
Void Put (int x1, int x2) // assign a value
{
A = x1;
B = x2;
}
Void Display (); // Display
Int addition (); // addition
Int substraction (); // Subtraction
Int multiplication (); // multiplication
Int division (); // triggered
};


Void JiSuanQi: Display () // Display
{
Cout <"show A =" <A <endl;
Cout <"show B =" <B <endl;
}
Int JiSuanQi: addition () // addition
{
C = A + B;
Return C;
}
Int JiSuanQi: substraction () // Subtraction
{
C = A-B;
Return C;


}
Int JiSuanQi: multiplication () // multiplication
{
C = A * B;
Return C;
}
Int JiSuanQi: division () // division
{
C = A/B;
Return C;
}
Int main () // main Function
{
Int x1, x2;
JiSuanQi calculation;
Cin> x1> x2;
Calculation. Put (x1, x2 );
Calculation. Display ();
Cout <"addition :";
Cout <x1 <"+" <x2 <"=" <calculation. addition () <endl;
Cout <"subtraction :";
Cout <x1 <"-" <x2 <"=" <calculation. substraction () <endl;
Cout <"multiplication :";
Cout <x1 <"*" <x2 <"=" <calculation. multiplication () <endl;
Cout <"Division :";
Cout <x1 <"/" <x2 <"=" <calculation. division () <endl;
Return 0;
}


Objects are the basic units of c ++ programming. classes describe the common attributes and behaviors of a type of problem. objects are class instances and objects are variables defined by classes; after an object is defined with a class, the object has all the properties of the class. That is to say, the data member of the class is the data member of the object, and the member function of the class is the member function of the object.
To create an object, you generally need to initialize the data members of the object and apply for the necessary storage space for the object. C ++ provides the necessary mechanism for object initialization, allowing users to write initialization programs, that is, constructors. The constructor is automatically called by the system. It cannot have a return value type or be labeled as void. constructor is generally declared as a public function and can be reloaded.
No constructor is used to define objects, that is, to automatically call a non-parameter constructor.

# Include <iostream>


Using namespace std;
Class Circle {
Private:
Float r;
Public:
Circle () {// No parameter construction
R = 0;
}
Circle (float x) {// construction with Parameters
R = x;
}
Float peri (){
Return 2 * r * 3.1415;
}
Float area (){
Return r * 3.1415;
}
};


Int main (int argc, char ** argv ){
Circle circle1, circle2 (2.0); // you don't need to write it if you don't have any parameters. Enclose the brackets.
Cout <"circle1" <endl;
Cout <"Perimeter" <circle1.peri () <"area" <circle1.area () <endl;

Cout <"circle2" <endl;
Cout <"Perimeter" <circle2.peri () <"area" <circle2.area () <endl;
Return 0;
}
Destructor is a program to be executed at the end of an object's life cycle. It is used to clear objects before they are deleted. The Destructor cannot have any return type or void. The Destructor does not have any parameters and cannot be overloaded. A class can have only one destructor.


Object replication is an important capability of c ++ programming. Copy the constructor,
It has the characteristics of a common constructor. It has only one parameter, and the parameter type is reference of this class object. The function should be designed to copy the values of known objects to the newly defined object of the same type.




A normal constructor is automatically called only when an object is created, and a copy constructor can be called automatically in the following example. When an old object is used to define a new object of the class, if the function parameter is an object, when the function is called;


# Include <iostream>


Using namespace std;
Class Point {
Private:
Float x, y;
Public:
Point (){
X = 0;
Y = 0;
Cout <"Running constructors without Parameters" <endl;
}
Point (float x1, float x2 ){
X = x1;
Y = x2;
// Cout <"coordinates of the output point" <x1 "," <x2 <endl;
Cout <"Running constructor with Parameters" <endl;

}
Point (Point & objb ){
X = objb. x;
Y = objb. y;
Cout <"constructor for running the replication function" <endl;

}
~ Point (){
Cout <"execute destructor" <endl;
}
Void Set (float a = 0, float B = 0 ){
X =;
Y = B;
}
Void Display (){
Cout <"the location of the point is: (" <x <',' <y <")" <endl;
}

};


Int main (int argc, char ** argv ){


Point;
A. Display ();
Point B (2.0, 2.0 );
B. Display ();
Point c ();
C. Display ();
A. Set (5, 5 );
A. Display ();
Return 0;
}

















































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.