C/C++(C++类与对象)

构造器(constructor)

1.与类名相同,无返回,被系统生成对象时自动调用,用于初始化。
2.可以有参数,构造器的重载,有默认参数。重载和默认参数不能同时出现,但是一定要包含标配(无参数的构造器),为了对象的午无参创建。
3.如果未提供任何构造器,系统默认提供一个无参的构造器。如果提供,则不再生成默认构造器。

#ifndef STACK_H
#define
class Stack
{
public:
    Stack()//构造器,
    {
        top = 0;
        space = new char[size];
    }
    Stack(int size = 100)//构造器与构造器之间是重载的关系
    {
        top = 0;
        space = new char[size];
        _size = size;
    }
public:
    //void init();
    bool isEmpty();
    bool isFull();
    void push(int data);
    int pop();
private:
    int space[1024];
    int top;
    int _size;
};
#endif

stack.cpp:

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

main.cpp

#include<iostream>
#include "stack.h"
using namespace std;
int main()
{
    Stack s;
    Stack s2(100);
    //s.init();
    for(char v = "a";!st.isFull()&& v != 'z'+1;v++)
    {
        st.push(v);
    }
    while(!s.isEmpty())
        cout<<s.pop()<<endl;
    return 0;
}

析构器(destructor)"~"

1.~与类名相同,无参,无返回。
2.对象消失的时候,自动被调用,用于对象销毁是的处理工作。
3.如果未提供任何构造器,系统会自动生成一个空析构器。

#ifndef STACK_H
#define
class Stack
{
public:
    Stack()//构造器,
    {
        top = 0;
        space = new char[size];
    }
    Stack(int size = 100)//构造器与构造器之间是重载的关系
    {
        top = 0;
        space = new char[size];
        _size = size;
    }
    ~Stack()
    {
        delete sapce;//适合于new生成的空间
    }
public:
    //void init();
    bool isEmpty();
    bool isFull();
    void push(int data);
    int pop();
private:
    int space[1024];
    int top;
    int _size;
};
#endif
#include<iostream>
#include "stack.h"
using namespace std;
int main()
{
    Stack s;
    Stack s2(100);
    //s.init();
    for(char v = "a";!st.isFull()&& v != 'z'+1;v++)
    {
        st.push(v);
    }
    while(!s.isEmpty())
        cout<<s.pop()<<endl;
    return 0;
}

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);

    return 0;
}

C++:

class Stu
{
    char *name;
    int age;
public:
    Stu()
    {
        name = new char[100];
    }
    ~Stu()
    {
        delete []name;//释放构造器中new出来的空间
    }
}
int main()
{
    Stu *ps = new Stu;
    strcpy(ps->name,"assassin");
    delete ps;//释放上述new出的空间

    return 0;
}
/*
Stu *p = new Stu;生成八个字节的空间,p指向8个字节,Stu的构造器生成100个字节,name指向这100个字节。delete ps,调用了析构函数,析构函数先释放100个字节的空间,再释放掉8个字节的空间。
*/

自实现string类
main.cpp

#include <iostream>
#include "myString.h"
using namespace std;

int main()
{
    string s;               //char * _str = "";
    string s2 = "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;
    return 0;
}

myString.h:

#ifndef STRING_H
#define STRING_H

class String
{
public:
    String();
    String(const char *s);
    char * c_str();//利用此函数输出
private:
    char * _str;
};

#endif // STRING_H

myString.cpp

#include<iostream>
#include "string.h"
#include "myString.h"
using namespace std;

String::String()
{
    _str = new char[1];
    *_str = '';//字符
}

String::String(const char *s)
{
    int len = strlen(s);
    _str = new char[len+1];
    strcpy(_str,s);
}
char * String::c_str()
{
    return _str;
}

改进:
myString.h文件

#ifndef STRING_H
#define STRING_H

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

#endif // STRING_H

myString.cpp文件:

#include<iostream>
#include "string.h"
#include "myString.h"
using namespace std;
#if 0
String::String()
{
    _str = new char[1];
    *_str = '';//字符
}

String::String(const char *s)
{
    int len = strlen(s);
    _str = new char[len+1];
    strcpy(_str,s);
}
#endif
String::String(const char *s)
{
    if(s == NULL)
    {
        _str = new char[1];
        *_str = '';
    }else{
        int len = strlen(s);
        _str = new char[len+1];
        strcpy(_str,s);
    }

}

char * String::c_str()
{
    return _str;
}
String::~String()
{
    delete []_str;
}

main.cpp:文件

#include <iostream>
#include "myString.h"
using namespace std;

int main()
{
    string s;               //char * _str = "";
    string s2 = "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;


    return 0;
}

    string *ps = new string("assassin");
    delete ps;
    string *ps1 = new string("assassin1");
    delete ps1;

ps指向new生成一个四个字节的空间char *,char *指向一串字符。delete ps时,先释放掉内部一串字符,再释放掉char *。

原文地址:https://www.cnblogs.com/intelwisd/p/8519989.html