C + + (C + + classes and objects)

Source: Internet
Author: User

Constructor (constructor)

1. Same as the class name, no return, automatically called when the object is generated by the system for initialization.
2. You can have parameters, overloads of constructors, and default parameters. Overloads and default parameters cannot be present at the same time, but it is important to include the standard (parameterless constructor) for the purpose of the object's noon and no parameter creation.
3. If no constructors are provided, the system provides a constructor without parameters by default. If provided, the default constructor is no longer generated.

#ifndef Stack_h#defineclassstack{ Public: Stack ()//constructor,{top =0; Space =New Char[Size]; } Stack (intSize = -)//The constructor is overloaded with the Builder{top =0; Space =New Char[Size];    _size = size; } Public://void init ();    BOOLIsEmpty ();BOOLIsfull ();voidPushintdata);intPop ();Private:intspace[1024x768];intTopint_size;};#endif

Stack.cpp:

#include<iostream>#inlcude "stack.h"#include<stdlib.h>#include<string.h>bool Stack::isEmpty(){    return0;}bool Stack::isFull(){    return1024;}void Stack::push(int data){    space[top++] = data;}int Stack::pop(){    return space[--top];}

Main.cpp

#include<iostream>#include "stack.h"usingnamespace std;int main(){    Stack s;    Stack s2(100);    //s.init();    for(char"a"‘z‘+1;v++)    {        st.push(v);    }    while(!s.isEmpty())        cout<<s.pop()<<endl;    return0;}
destructor (destructor) "~"

The 1.~ is the same as the class name, with no parameters and no return.
2. When the object disappears, it is automatically called and used for object destruction.
3. If no constructors are provided, an empty destructor is automatically generated.

#ifndef Stack_h#defineclassstack{ Public: Stack ()//constructor,{top =0; Space =New Char[Size]; } Stack (intSize = -)//The constructor is overloaded with the Builder{top =0; Space =New Char[Size];    _size = size; } ~stack () {DeleteSAPCE;//Suitable for new generation of space} Public://void init ();    BOOLIsEmpty ();BOOLIsfull ();voidPushintdata);intPop ();Private:intspace[1024x768];intTopint_size;};#endif
#include<iostream>#include "stack.h"usingnamespace std;int main(){    Stack s;    Stack s2(100);    //s.init();    for(char"a"‘z‘+1;v++)    {        st.push(v);    }    while(!s.isEmpty())        cout<<s.pop()<<endl;    return0;}

C:

struct Stu{    char *name;    int age;}int main(){    Stu *ps = (Stu*)malloc(sizeof(Stu));    ps->name = (char *)malloc(100);//给name也要申请空间    strcpy(ps->name,"xxx");    free(ps->name);//释放的时候也要从里向外释放。    free(ps);    return0;}

C++:

class Stu{    char *name;    int age;public:    Stu()    {        newchar[100];    }    ~Stu()    {        delete []name;//释放构造器中new出来的空间    }}int main(){    new Stu;    strcpy(ps->name,"assassin");    delete ps;//释放上述new出的空间    return0;}/*Stu *p = new Stu;生成八个字节的空间,p指向8个字节,Stu的构造器生成100个字节,name指向这100个字节。delete ps,调用了析构函数,析构函数先释放100个字节的空间,再释放掉8个字节的空间。*/

Self-Implementing String class
Main.cpp

#include <iostream>#include "myString.h"usingnamespace std;int main(){    string s;               //char * _str = "";    "assassin"//char * str = "xxxx";    cout<<s.c_str()<<endl;    cout<<s2.c_str()<<endl;    String ss;    "intelwisd";    cout<<ss.c_str()<<endl;    cout<<sss.c_str()<<endl;    return0;}

MyString.h:

#ifndef STRING_H#define STRING_Hclass String{public:    String();    String(constchar *s);    char * c_str();//利用此函数输出private:    char * _str;};#endif // STRING_H

MyString.cpp

#include<iostream>#include "string.h"#include "myString.h"usingnamespace std;String::String(){    newchar[1];    ‘\0‘;//字符}String::String(constchar *s){    int len = strlen(s);    newchar[len+1];    strcpy(_str,s);}char * String::c_str(){    return _str;}

Improved:
MyString.h file

#ifndef STRING_H#define STRING_H#if 0class String{public:    String();    String(const char *s);    char * c_str();private:    char * _str;};#endifclass String{public:    //String();    String(constchar *s = NULL);//无参的形式包含在里面    char * c_str();    ~String();private:    char * _str;};#endif // STRING_H

MyString.cpp file:

#include <iostream>#include "string.h"#include "myString.h"using namespaceStd#if 0string::string (){_str = new char[1];*_str = ' + ';//character}string::string (const char *s){int len = strlen (s);_str = new char[len+1];strcpy (_str,s);}#endifString::string (Const Char*s) {if(s = = NULL) {_str =New Char[1]; *_str =' /'; }Else{intLen = strlen (s); _str =New Char[Len+1];    strcpy (_str,s); }}Char* STRING::C_STR () {return_str;} String::~string () {Delete[]_str;}

Main.cpp: File

#include <iostream>#include "myString.h"usingnamespace std;int main(){    string s;               //char * _str = "";    "assassin"//char * str = "xxxx";    cout<<s.c_str()<<endl;    cout<<s2.c_str()<<endl;    String ss;    String sss("intelwisd");    cout<<ss.c_str()<<endl;    cout<<sss.c_str()<<endl;    return0;}
    new string("assassin");    delete ps;    new string("assassin1");    delete ps1;

PS points to new to generate a four-byte space char , char points to a string of characters. Delete PS, release the inner string first, and then release char *.

C + + (C + + classes and objects)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.