From 137e8c68cda69ec0f256f3786f944cd2cb7d4ffe Mon Sep 17 00:00:00 2001 From: ivs Date: Thu, 7 Nov 2024 12:18:48 +0800 Subject: [PATCH] Comments T9 --- T9Q1_1/main.cpp | 23 ++++++++++++++++++++++- T9Q2/main.cpp | 3 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/T9Q1_1/main.cpp b/T9Q1_1/main.cpp index d379e81..eb322af 100644 --- a/T9Q1_1/main.cpp +++ b/T9Q1_1/main.cpp @@ -11,6 +11,17 @@ public: } virtual void print(); friend int doubleIt(A a); + + // Question 1.B + // Option 1: Member method + A operator+(A z) { + const int new_a = a + z.a; + const int new_b = b + z.b; + return {new_a, new_b}; + } + // Option 2: Non-member method + friend A operator+(const A& z, const A& y); + // Question 1.B }; class B : public A { @@ -37,8 +48,18 @@ void B::input(float x, float y) { int doubleIt(A a) { return a.a * a.a; } +// Question 1.B +// Option 2: Non-member method +A operator+(const A& z, const A& y) { + const int new_a = z.a + y.a; + const int new_b = z.b + y.b; + return {new_a, new_b}; +} +// Question 1.B + + int main() { - A a1(10, 20), *ptr; + A a1(10, 20), *ptr; // Try put *ptr in a new line, as "B *ptr;" to see what is happening; B b1; b1.input(7.5, 3.142); diff --git a/T9Q2/main.cpp b/T9Q2/main.cpp index 4d9fa56..412a8c7 100644 --- a/T9Q2/main.cpp +++ b/T9Q2/main.cpp @@ -3,7 +3,10 @@ using namespace std; class BC { public: + // Inheritance in C++ is not similar to Java. The method above will not be automatically overrride void show(void) { cout << " \n I am in base class.."; } + // Unless we add the keyword virtual on it. Comment the line above and uncomment the line below to test. +// virtual void show(void) { cout << " \n I am in base class.."; } }; class DC : public BC { public: