C to C + + fast over C struct to class

Source: Internet
Author: User

Remember the structure of C language?
struct point{
Double X;
Double y;
};
The code above is a "structural template" and we use it to create a type called point.

After that, we can declare the point type just like a variable declaring a primitive type:
Point ob;

An OB is called an "instance" of a struct point.

and when we int n; , n is a "variable" of type int.



A struct is a composite type, but it differs from an array that is also a composite type:


<!--array manipulation tips--

Char name[20];
We declare an array as such, and each name[i] (0 <=i < 20) is a variable of a separate char type. They are called "elements" of an array.

It is worth mentioning that the memory space occupied by each element of the same array is contiguous.

This allows us to traverse (retrieve) the entire array at a very fast speed:

int line[] = {2, 5, 6, 7, 8, 12,-5,-32}; 1
int len = sizeof (line)/sizeof (int); 2
for (int i = 0; i < len; i++)
Line[i] =-line[i];

The above statement iterates through the array, and the values of each element are reversed.

The 1th statement shows that the array can be automatically allocated according to the data you fill out when you declare it, but once assigned, the size of the array is fixed.

The 2nd statement can directly determine the number of elements in the array, so there is no need to count each.
sizeof is an operator and is used similar to a function. When the parameter is an array name, the number of bytes of memory is given to the entire array, and when the argument is a type, the number of bytes of that type variable is obtained.

For string arrays, initialization has a more convenient method:

Char name[] = "Always Laugh";

The name is then automatically assigned to an array of length 13 (Don't forget the ' + ').


In addition to the previous method of using the sizeof operator to take the number of elements, the Strlen function in the header file <string.h> (shi<cstring> in C + +) makes it easier to take the string length:

Char name[] = "Always Laugh";
int len = strlen (name);
for (int i = 0; i < len; i++)
name[i]++;

This code moves each element of the name array "Back" in the character table.

It is important to note that strlen can only handle string arrays of char, and not for Int.

And it ends with the ' + ' character at the end of the string, so it's often not the size of the array.



<!--class--

Now introduce the concept of "class" in C + +, which is very similar to a struct.

Let's start by writing a program that uses structs:

#include <iostream>

using namespace Std;

struct point{//1 Structural template

Double X; Members

Double y;

};

int main ()

{

Point P; 2 Declaration of the struct

p.x = 3.2; 3 Use of structural bodies

P.Y = 8.9;

cout << "(" << p.x << "," << p.y << ")" << Endl; Use of the structure body

return 0;

}

The structure of the template and function of the definition of the same nature (followed by the "class" is also the same), just specify the structure of the struct/class and work away from, do not allocate memory.

When we use a struct object, we always use its members. “.” This operator is used to connect an instance of a struct and its members.

The name of the instance can be defined freely, just like the variable name, but instances of the same type always have the same members, and are identical to the template.

That is, instances initialized with the same template have the same members.


When a class is introduced, a large change occurs:

#include <iostream>

using namespace Std;

Class point{

Private

Double X;

Double y;

Public

SetPoint (double m, double N) {

x = m; y = n;

}


Printpoint () {

cout << "(" << x << "," << y << ")" << Endl;

}

};


int main ()

{

Point P;

P.setpoint (3.2, 8.9);

P.printpoint ();

return 0;

}


The effect of this program and the structure is the same, but the complex place is concentrated in the template definition section of the class.

There are a lot of strange things, private,public, and even functions.


This is because, for a class, not only variables can be members, but functions can also.


And when you want to use the following statement in the main function:

p.x = 2.2; P.Y = 1.1;

You will find that this is not allowed.

The reason is that the keyword private (private), which protects the x and Y members so that they do not appear outside the template area.

Note that it is not possible to appear, not just to assign a value. This means cout << p.x; is not allowed.


The members defined by the public (common) keyword can be exposed to the outside world, as P.setpoint (3.2, 8.9) in the main function, and P.printpoint ();


setting x and Y to private members is for the security of the data:

int main ()

{

Point P;

P.setpoint (3.2, 8.9);

P.printpoint ();

return 0;

}

This is just the main function, we can not see the P this class contains what kind of or how many private members, but also do not know the member is called X/Y.

They can actually be seen in the class template, but most of the classes we use in the project are not visible to the class template, and they are stored in different files.

In this way, we only need to know what variables the public member functions of the class accept and what they do, and learn how to invoke them. As for the specifics, internal logic, we don't need to know.

This is the "encapsulation", one of the three main features of C + +.


As you can see, private member variables may appear directly in the member function because they are in the same scope (the curly brackets of the class template).

Do you have a private member function? Of course. It can be called by other private member functions or public member functions.

In summary, only public members (which can appear outside the template area) and the "." Conjunction Because the declaration and use of an "instance" is in the external


Nutshell:

Members include private members and public members.

Members are divided into member variables and member functions.



<!--Constructors--

Class point{

Private

Double X;

Double y;

Public

Point () {

}

Point (double m, double N) {

x = m; y = n;

}

SetPoint (double m, double N) {

x = m; y = n;

}

Printpoint () {

cout << "(" << x << "," << y << ")" << Endl;

}

};


It's still the same class, with two more weird functions. Yes, the name of the function is the same as the class name, and they do not contain the return value.

With them, we can declare p like this in the main function.

int main ()

{

Point P (3.2, 8.9);

P.printpoint ();

return 0;

}

The constructor is called during the initialization of the instance, so that we can assign a value to the member while initializing, and setpoint is used to change the value of the member.

Why is the point function declared two times? Do you remember function overloading? We have overloaded it.

This allows us to either initialize it when declaring an instance, or not.

Because constructors are always executed. Point P; and point P (); no difference whatsoever.



<!--a somewhat complex sample program--

#include <iostream>

#include <cstring>

using namespace Std;

struct friend{

Char name[20];

Friend* Nest;


Friend (char* pn) {

strcpy (name, PN);

Nest = NULL;

}

};


struct friendlist{

friend* Head;

Friend* mov;

int number;

bool none;


Friendlist (friend* pri) {

head = mov = PRI;

Number = 1;

none = 0;

}

void Retop () {

mov = head;

none = 0;

}


void befriend (friend* someone) {

friend* temp = mov->nest;

Mov->nest = someone;

Someone->nest = temp;

number++;

}


BOOL Que (Friend *f) {

if (none) {

cout << "You got no friends.\n\n";

Retop ();

return 0;

}

if (!mov->nest) none = 1;

Char ch;

while (GetChar ()! = ' \ n ') continue;

cout << Friends with

<< mov->name << "?" << Endl

<< "Y to YES and N to NO" << Endl;

CIN >> ch;

if (ch = = ' Y ') {

cout << "You ve been friend with" << mov->name << "!\n\n";

Befriend (f);

Retop ();

return 0;

}else{

mov = mov->nest;

return 1;

}

}


void End ()

{

friend* temp;

while (number--) {

temp = head->nest;

Delete (head);

Head = temp;

}

}

};


int main ()

{

Char name[20] = "Always Laugh";

Friend *me = new Friend (name);

Friendlist myfriend (Me);

int T = 4;

while (t--) {

cout << "Enter Your name:";

CIN >> name;

friend* you = new Friend (name);

cout << "Hello" << you->name << "!" << Endl;

while (Myfriend.que (you));

}

return 0;

}


This sample program is very difficult for people who are just in touch with C + + and is close to a lesson set. Because the inner contact of the class is complex.

It also uses dynamic memory allocations (such as the new and delete operators), and the contents of linked lists (which are unfamiliar to everyone).

This is a friend's program, you might as well compile and run, see the effect, and then try to understand the code (in order to avoid cross-border access, please do not enter too weird).





C to C + + fast over C struct to class

Related Article

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.