A simple class for C + +

Source: Internet
Author: User

Starting with the basic data type:

In general, the basic data types in C + + have int, char,,,,

However, these data types are limited and are self-contained in C + + and lack flexibility

C + + provides a way to define a custom type----use structs (struct)

But the structure also has his limitations:

1 How data is organized in a struct

2 The data in the struct has no access restrictions, which makes it easy to modify the data arbitrarily

3 structure data is not inherited

In order to solve these problems. C + + provides a class

As a result, we'll know:

So we can understand the class in C + +

Collection of data + operation of data =class

Some misconceptions about C + + and Java:

#include <cstdlib>

#include <iostream>

using namespace Std;

Class Student

{

/**

Personal advice put the public on the back there's a problem yellow

**/

Public:

char* getName ()//Array life

{

return name;

}

void SetName (char* c)

{

name = &c;

}

/** constructor function

**/

Student ()

{

age=1;

gender=0;

}

~student ()

{

cout << "123";

}

void Dispaly ()

{

cout << "name=:" << name << Endl

<< "age=:" << age << Endl;

}

Private:

Char name[20];

int age;

BOOL gender;

} ;

int main (void)

{

Student s;

S. dispaly ();

System ("PAUSE");

return exit_success;

}

Suggestions:

The fewer statements in the main () function, the better, it's best to put all the definitions in the *.h file

This can separate the representation from the definition;

A simple class for C + +

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.