c++基本知识类和对象构造函数析构函数拷贝构造函数


#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<list>
/*
类和对象
构造函数
析构函数
拷贝构造函数
new  delete
引用
内联
重载
const
*/
#if 0
class A
{
public:
    A(int i = 0) :m_i(i) {}
    //int GetI() { return m_i; }
    const int &GetI()
    {
        return m_i;
    }
    void Print() { cout << m_i << endl; }
private:
    int m_i;
};

void main()
{
    A a(10), b(20);
    //a.m_i = 20;
    cout << a.GetI() << endl;  //cout<<a.m_i<<endl;
    //a.GetI() = 20;
    //a.GetI() = 30;
    a.Print();
    cout << a.GetI() + b.GetI() << endl;
}
#endif
#if 0
class Student
{
public:
    Student(int num = 1001, const char* name = "") :m_num(num)
    {
        m_name = new char[strlen(name) + 1];
        strcpy(m_name, name);
    }
    /*
    默认拷贝构造-浅拷贝  依次复制
    Student(const Student& s) :m_num(s.m_num),m_name(s.m_name)
    {
        cout << "Student(s)" << endl;
    }
    */
    //深拷贝 拷贝资源
    Student(const Student& s) :m_num(s.m_num)
    {
        m_name = new char[strlen(s.m_name) + 1];
        strcpy(m_name, s.m_name);
    }
    void print()
    {
        cout << m_num << " "<<m_name<<endl;
    }
    ~Student()
    {
        delete[]m_name;
        m_name = nullptr;
        cout << "~Student" << endl;
    }
private:
    int m_num;
    char* m_name;
};
void main()
{
    Student s1(1002,"zhangsan");
    Student s2(s1);
    s1.print();
    s2.print();
    //Student *p =  new Student; //p指向new出来的对象
    //Student* q = (Student*)malloc(sizeof(Student));
}
#endif
#if 0
class Node
{
public:
    Node():m_next(nullptr) {}
    Node(int v) :m_value(v), m_next(nullptr) {}
    int m_value;
    Node* m_next;
};
//Node *p = new Node(1);
class List
{
public:
    List() :m_pHead(nullptr),m_size(0) {}
    void Push_back(int v);
    void Push_front(int v);
    void Insert(int pos,int v);
    void Print();
    void Pop_back();
    void Pop_front();
    void Del(int pos);
    void Sort();
    void Reverse();
    int Length();
    bool IsEmpty()const;
    Node* front();  //返回第一个节点
    Node* back(); //返回最后一个节点
    void Merge(List& l); //合并链表
private:
    Node* m_pHead;
    int m_size;
};
void List::Print()
{
    Node* p = m_pHead;
    while (p)
    {
        cout << p->m_value << "->";
        p = p->m_next;
    }
    cout << "nul" << endl;
}
void List::Push_front(int v)
{
    Node* p = new Node(v);
    p->m_next = m_pHead;
    m_pHead = p;
    m_size++;
}

void main()
{
    List l;
    l.Push_front(1);
    l.Push_front(2);
    l.Push_front(3);
    l.Push_front(4);    
    l.Print();
}

#endif


/*
运算符重载
友元
*/
/*
1.一个普通函数可以作为类的友元,此时在这个函数中就可以访问类的私有数据成员
*/
#if 0
class A
{
public:
    A(int i = 0,int j = 0) :m_i(i),m_j(j) {}
    const int& GetI() { return m_i; }
    const int& GetJ() { return m_j; }
    friend void Add(A& a, A& b);
private:
    int m_i;
    int m_j;
};
void Add(A& a, A& b)
{
//    int i = a.GetI() + b.GetI();
//    int j = a.GetJ() + b.GetJ();
    int i = a.m_i + b.m_i;
    int j = a.m_j + b.m_j;
    cout << i << " " << j << endl;
}

void main()
{
    A a(10,10);
    A b(20, 20);
    Add(a, b);
}
#endif
#if 0
class B; //前向引用声明
class A
{
public:
    A(int i = 0) :m_i(i) {}
    friend void Add(A& a, B& b);
private:
    int m_i;
};
class B
{
public:
    B(int j = 0) :m_j(j) {}
    friend void Add(A& a, B& b);
private:
    int m_j;
};
void Add(A& a, B& b)
{
    cout << a.m_i + b.m_j << endl;
}
void main()
{
    A a(10);
    B b(20);
    Add(a, b);
}
#endif
//一个类的成员函数可以作为另外一个类的友元
#if 0
class B;
class A
{
public:
    A(int i = 0) :m_i(i) {}
    void fn(B& b);
private:
    int m_i;
};
class B
{
public:
    B(int j = 0) :m_j(j) {}
    friend void A::fn(B& b);
private:
    int m_j;
};
void A::fn(B& b)
{
    cout << m_i + b.m_j << endl;
}
void main()
{
    A a(1);
    B b(4);
    a.fn(b);
}
#endif

/*
友元类
友元不能传递
友元是单向的
友元没有继承
*/
#if 0
class A
{
public:
    A(int i = 0) :m_i(i) {}
    friend class B;
private:
    int m_i;
};
class B
{
public:
    B(int j = 0) :m_j(j) {}
    void fn(A& a) { cout << a.m_i << endl; }
    void test(A &a) { cout << a.m_i << endl; }
    void gn() {}
    friend class C;
private:
    int m_j;
};
class C
{
public:
    void ff(B& b) { cout << b.m_j << endl; }
//    void fff(A& a) { cout << a.m_i << endl; }
};
void main()
{
    A a(30);
    B b(40);
    C c;
    b.fn(a);
    b.test(a);
    c.ff(b);
}
#endif


//运算符重载
/*
值返回:运算符组成的表达式只能放在=的右边
引用返回:表达式可以放在=的左边
*/
#if 0
class A
{
public:
    A(int i = 0) :m_i(i) {}
    int fn(A& b)
    {
        return m_i + b.m_i;
    }
    friend int ff(A& a, A& b);
private:
    int m_i;
};
int ff(A& a, A& b)
{
    return a.m_i + b.m_i;
}
void main()
{
    A a(10);
    A b(10);
    //a + b;  //a.+(b)  +(a,b)
    int n = a.fn(b);
    cout << n << endl;
    n = ff(a, b);
    cout << n << endl;
}
#endif
#if 0
void main()
{
    int a = 10, b = 20;
    int c = 0;
    //a + b = c;
    ++a = c;
    (a = b) = c;
}
#endif

class A
{
public:
    A(int i = 0) :m_i(i) {}
/*    A operator+(A& b)
    {
        return m_i + b.m_i;
    }
    */
    A& operator+(A& b)
    {
        m_i = m_i + b.m_i;
        return *this;
    }
    void print()
    {
        cout << m_i << endl;
    }
private:
    int m_i;
};
void main()
{
    A a(1);
    A b(3);
    A c(10);
    (a + b).print();
    a.print();
    a + b = c;
    a.print();
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<algorithm>  //通用算法
#if 0
class STR
{
public:
    STR();
    STR(const char* str);
    STR(int n, char c);
    void print()
    {
        cout << "m_str = " << m_str << endl;
    }
    ~STR()
    {
        delete[]m_str;
        m_str = nullptr;
    }
    STR& operator=(const STR& t)  //深赋值
    {
        if (this == &t)
            return *this;
        delete[]m_str;
        m_str = new char[strlen(t.m_str) + 1];
        strcpy(m_str, t.m_str);
        return *this;
    }
    STR(const STR& s)
    {
        m_str = new char[strlen(s.m_str) + 1];
        strcpy(m_str, s.m_str);
    }
    STR operator+(const STR& s);
    char& operator[](int index)
    {
        
        if (index < strlen(m_str))
        {
            m_str[index] = 'r';
            return m_str[index];
        }
    }
    const char& operator[](int index)const
    {
        cout << "const []const" << endl;
        if (index < strlen(m_str))
            return m_str[index];
    }
    bool operator<(const STR& t)const ;
    bool operator==(const STR& t)const;
    friend ostream& operator<<(ostream& os, const STR& s);
private:
    char* m_str;
};
STR::STR()
{
    m_str = new char[1];
    *m_str = '\0';
}
STR::STR(const char* str)
{
    m_str = new char[strlen(str) + 1];
    strcpy(m_str, str);
}
STR::STR(int n, char c)
{
    m_str = new char[n + 1];
    memset(m_str, c, n);
    m_str[n] = '\0';
}
ostream& operator<<(ostream& os, const STR& s)
{
    os << s.m_str;
    return os;
}
// a("123") b("456") "123456"
STR STR::operator+(const STR& s)
{
    char* temp = new char[strlen(m_str) + strlen(s.m_str) + 1];
    strcpy(temp, m_str);
    strcat(temp, s.m_str);

    STR t(temp);
    delete[]temp;
    temp = nullptr;
    return t;
}
bool STR::operator<(const STR& t)const
{
    cout << "<" << endl;
    if (strcmp(m_str, t.m_str) <  0)
        return true;
    else
        return false;
}
bool STR::operator==(const STR& t)const
{
    if (strcmp(m_str, t.m_str) == 0)
        return true;
    else
        return false;
}
void main()
{
    STR s;
    STR s1("123456");
    STR s2(4, 'k');
    //s1.print();
    //s2.print();
    cout << "s1 = " << s1 << endl;
    cout << "s2 = " << s2 << endl;
    s = s1;
    cout << "s = " << s << endl;
    //    s1 = s2;
    //    cout << "s1 = " << s1 << endl;
    s = s1 + s2; //123456kkkk
    cout << "s = " << s << endl;
    cout << s[3] << endl;  //s.[](3)
    s[3] = 'k'; //
    cout << s << endl;//123k56kkkk
    const STR t("abcdefg");
    cout << t[5] << endl;

    cout << (s < t) << endl;  //s.<(t)
    cout <<"s < 111:"<< (s < "111") << endl;

    cout << "s == t : " << (s == t) << endl;
}
#endif
#if 0
class A
{
public:
    A() 
    {
        m_i = new char[10];
        strcpy(m_i, "hello");
        cout << "A" << endl; 
    }
    ~A()
    {
        if (m_i != nullptr)
        {
            delete[]m_i;
            m_i = nullptr;
        }
        cout << "~A" << endl;
    }
    void* operator new(size_t size)
    {
        cout << "operator new" << endl;
        return malloc(size);
    }
    void operator delete(void* p,size_t size)
    {
        cout << "operator delete" << endl;
        free(p);
    }
private:
    char *m_i;
    //double m_j;
};
//void *malloc(size_t size)  
//int *p = (int*)malloc(4)
void main()
{
    A* p = new A;
    delete p;
}
#endif
//a+b
//int a,b; a + b;
#if 0
class A
{
public:
    A(int i = 0) :m_i(i) {}
    A& operator+=(const A& b)
    {
        m_i = m_i + b.m_i;
        return *this;
    }
    void print()
    {
        cout << m_i << endl;
    }
private:
    int m_i;
};
void main()
{
    A a(10),b(20);
    a += b; //a = a + b;
    a.print();
}
#endif

#if 0
class Great
{
public:
    bool operator()(int& a, int& b)
    {
        return a > b;
    }
};
void print(int n)
{
    cout << n << " ";
}
class Print
{
public:
    void operator()(int n)
    {
        cout << n << " ";
    }
};
void main()
{
    int arr[] = { 1,5,4,7,6,9,8,0,4,3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n,Great());  //函数对象(仿函数)
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
    //for_each(arr, arr + n, print);
    for_each(arr, arr + n, Print());
    cout << endl;
}
#endif
#if 0
class Plus
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};
void main()
{
    cout << Plus()(5, 7) << endl;

}
#endif
/*
关系运算符: 
*/
#if 0
int m_count;
class StudentInfo
{
public:
    StudentInfo(int num = 1001, string name = "", int c = 0) :m_num(num), m_name(name)//,m_count(c)
    {
        m_count++;
    }
    void print()
    {
        cout << m_num << " " << m_name << " " << m_count << endl;
    }
private:
    int m_num;
    string m_name;
    //int m_count;
};
void main()
{
    StudentInfo s1(1001, "aaa");
    s1.print();
    StudentInfo s2(1002, "bbb");
    StudentInfo s3(1003, "ccc");
    s1.print();
    s3.print();
    m_count = 10;
    s1.print();
}
#endif

class StudentInfo
{
public:
    StudentInfo(int num = 1001, string name = "", int c = 0) 
        :m_num(num)
    {
        m_count++;
    }
    void print()
    {
        cout << m_num << " " << m_count << endl;
    }
private:
    int m_num;
    static int m_count;  //引用性声明
};
int StudentInfo::m_count ; //定义性声明
void main()
{
    cout << sizeof(StudentInfo) << endl;
    StudentInfo s1(1001);
    s1.print(); //1
    StudentInfo s2(1002);
    StudentInfo s3(1003);
    StudentInfo s4(1004);
    s1.print();//4
    s4.print();//4
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值