C++学习(2)-类和对象

2020-08-11  本文已影响0人  su945

1.类成员的可访问范围

class className {
private:
    私有属性和函数
public:
    公有属性和函数
protected:
    保护属性和函数
};

如过某个成员前面没有上述关键字,则\color{red}{缺省地被认为是私有成员}

2.成员函数的重载及参数缺省

#include <iostream>
using namespace std;
class Location {
private:
    int x, y;
public:
    void init(int x = 0, int y = 0);
    void valueX(int val) { x = val; }
    int valueX() { return x; }
};
void Location::init(int X, int Y)
{
    x = X;
    y = Y;
}
int main() {
    Location A, B;
    A.init(5);
    A.valueX(5);
    cout << A.valueX(); //5
    return 0;
}

3.构造函数



  1. 构造函数执行必要的初始化工作,有了构造函数,就不
    必专门再写初始化函数,也不用担心忘记调用初始化函数。
  2. 有时对象没被初始化就使用,会导致程序出错。
    3)构造函数最好是public的,private构造函数不能直接用来初始化对象

4.构造函数在数组中的使用

class CSample {
    int x;
public:
    CSample() {
        cout << "Constructor 1 Called" << endl;
    }
    CSample(int n) {
        x = n;
        cout << "Constructor 2 Called" << endl;
    }
};
int main() {
    CSample array1[2];
    cout << "step1" << endl;
    CSample array2[2] = { 4,5 };
    cout << "step2" << endl;
    CSample array3[2] = { 3 };
    cout << "step3" << endl;
    CSample * array4 =
        new CSample[2];
    delete[]array4;
    return 0;
    /*
    Constructor 1 Called
    Constructor 1 Called
    step1
    Constructor 2 Called
    Constructor 2 Called
    step2
    Constructor 2 Called
    Constructor 1 Called
    step3
    Constructor 1 Called
    Constructor 1 Called
    */
}

class Test {
public:
    Test(int n) { } //(1)
    Test(int n, int m) { } //(2)
    Test() { } //(3)
};
Test array1[3] = { 1, Test(1,2) };
// 三个元素分别用(1),(2),(3)初始化
Test array2[3] = { Test(2,3), Test(1,2) , 1 };
// 三个元素分别用(2),(2),(1)初始化
Test * pArray[3] = { new Test(4), new Test(1,2) };
//两个元素分别用(1),(2) 初始化

5.复制构造函数

  1. 当用一个对象去初始化同类的另一个对象时。
Complex c2(c1);
Complex c2 = c1; //初始化语句,非赋值语句
  1. 如果某函数有一个参数是类 A 的对象,
    那么该函数被调用时,类A的复制构造函数将被调用。
class A
{
public:
    A() { };
    A(A & a) {
        cout << "Copy constructor called" << endl;
    }
};
void Func(A a1) { }
int main() {
    A a2;
    Func(a2);
    return 0;
}
  1. 如果函数的返回值是类A的对象时,则函数返回时,A的复制构造函数被调用:
class A
{
public:
    int v;
    A(int n) { v = n; };
    A(const A & a) {
        v = a.v;
        cout << "Copy constructor called" << endl;
    }
};
A Func() {
    A b(4);
    return b;
}
int main() {
    cout << Func().v << endl; return 0;
}

6.类型转换构造函数

 定义转换构造函数的目的是实现类型的自动转换。
 只有一个参数,而且不是复制构造函数的构造函数,一般就可以看作是转换构造函数。
 当需要的时候,编译系统会自动调用转换构造函数,建立一个无名的临时对象(或临时变量)。

class Complex {
public:
    double real, imag;
    Complex(int i) {//类型转换构造函数
        cout << "IntConstructor called" << endl;
        real = i; imag = 0;
    }
    Complex(double r, double i) { real = r; imag = i; }
};
int main()
{
    Complex c1(7, 8);
    Complex c2 = 12;
    c1 = 9; // 9被自动转换成一个临时Complex对象
    cout << c1.real << "," << c1.imag << endl;
    return 0;
}

class Complex {
public:
    double real, imag;
    explicit Complex(int i) {//显式类型转换构造函数
        cout << "IntConstructor called" << endl;
        real = i; imag = 0;
    }
    Complex(double r, double i) { real = r; imag = i; }
};
int main() {
    Complex c1(7, 8);
    Complex c2 = Complex(12);
    c1 = 9; // error, 9不能被自动转换成一个临时Complex对象
    c1 = Complex(9) //ok
        cout << c1.real << "," << c1.imag << endl;
    return 0;
}

7.析构函数


上一篇 下一篇

猜你喜欢

热点阅读