OOP HomeWork
This commit is contained in:
128
oop_hw5/hw5/main.cpp
Normal file
128
oop_hw5/hw5/main.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A(int num) : data(num) {
|
||||
cout << "A constructed with data = " << data << endl;
|
||||
}
|
||||
void AFuncs() {
|
||||
cout << "This is A's public function!" << endl;
|
||||
}
|
||||
A(const A& other) : data(other.data) {
|
||||
cout << "A copy constructor called." << endl;
|
||||
}
|
||||
A& operator=(const A& other) {
|
||||
if (this != &other) {
|
||||
data = other.data;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~A() {
|
||||
cout << "A destructor called." << endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
int data;
|
||||
};
|
||||
|
||||
class B {
|
||||
public:
|
||||
B(int num) : value(num) {
|
||||
cout << "B constructed with value = " << value << endl;
|
||||
}
|
||||
void BFuncs() {
|
||||
cout << "This is B's public function!" << endl;
|
||||
}
|
||||
B(const B& other) : value(other.value) {
|
||||
cout << "B copy constructor called." << endl;
|
||||
}
|
||||
B& operator=(const B& other) {
|
||||
if (this != &other) {
|
||||
value = other.value;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~B() {
|
||||
cout << "B destructor called." << endl;
|
||||
}
|
||||
|
||||
protected:
|
||||
int value;
|
||||
};
|
||||
|
||||
// === ԭʼ<D4AD><CABC><EFBFBD>̳а汾 ===
|
||||
class C1 : public A, private B {
|
||||
public:
|
||||
C1(int num1, int num2, int y) : A(num1), B(num2), yyy(y) {
|
||||
cout << "C1 constructed with yyy = " << yyy << endl;
|
||||
}
|
||||
void MyFuncs() {
|
||||
BFuncs();
|
||||
cout << "This function calls B::BFuncs() !" << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int yyy;
|
||||
};
|
||||
|
||||
// === <20><><EFBFBD>̳<EFBFBD> + <20><><EFBFBD>ϰ汾 ===
|
||||
class C2 : public A {
|
||||
public:
|
||||
C2(int num1, int num2, int y)
|
||||
: A(num1), b(num2), yyy(y) {
|
||||
cout << "C2 constructed with yyy = " << yyy << endl;
|
||||
}
|
||||
|
||||
C2(const C2& other)
|
||||
: A(other), b(other.b), yyy(other.yyy) {
|
||||
cout << "C2 copy constructor called." << endl;
|
||||
}
|
||||
|
||||
C2& operator=(const C2& other) {
|
||||
if (this != &other) {
|
||||
A::operator=(other);
|
||||
b = other.b;
|
||||
yyy = other.yyy;
|
||||
}
|
||||
cout << "C2 assignment operator called." << endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~C2() {
|
||||
cout << "C2 destructor called." << endl;
|
||||
}
|
||||
|
||||
void MyFuncs() {
|
||||
b.BFuncs();
|
||||
cout << "This function calls B::BFuncs() !" << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
B b;
|
||||
int yyy;
|
||||
};
|
||||
|
||||
// === <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ===
|
||||
int main() {
|
||||
cout << "=== <20><><EFBFBD>̳в<CCB3><D0B2><EFBFBD> ===" << endl;
|
||||
C1 obj1(10, 20, 30);
|
||||
obj1.AFuncs();
|
||||
obj1.MyFuncs();
|
||||
cout << endl;
|
||||
|
||||
cout << "=== <20><><EFBFBD>̳<EFBFBD> + <20><><EFBFBD>ϲ<EFBFBD><CFB2><EFBFBD> ===" << endl;
|
||||
C2 obj2(100, 200, 300);
|
||||
obj2.AFuncs();
|
||||
obj2.MyFuncs();
|
||||
|
||||
cout << "--- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ---" << endl;
|
||||
C2 obj3 = obj2;
|
||||
|
||||
cout << "--- <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> ---" << endl;
|
||||
C2 obj4(1, 2, 3);
|
||||
obj4 = obj3;
|
||||
|
||||
cout << "--- <20><><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>ʼ ---" << endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user