NTU-OOP-Tuts-2024-Cpp/T9Q1_1/main.cpp

50 lines
835 B
C++
Raw Normal View History

2024-11-05 10:34:51 +08:00
#include <iostream>
using namespace std;
class A {
protected:
int a, b;
public:
A(int x, int y) {
a = x;
b = y;
}
virtual void print();
friend int doubleIt(A a);
};
class B : public A {
private:
float p, q;
public:
B(int m, int n, float u, float v) : A(m, n){
p = u;
q = v;
}
B() : A(0, 0) { p = q = 0; }
void input(float u, float v);
virtual void print(float);
};
void A::print(void) {
cout << "A values : " << a << " " << b << "\n";
}
void B::print(float) {
cout << "B values : " << p << " " << q << "\n";
}
void B::input(float x, float y) {
p = x; q = y;
}
int doubleIt(A a) { return a.a * a.a; }
int main() {
A a1(10, 20), *ptr;
B b1;
b1.input(7.5, 3.142);
ptr = &a1;
ptr->print();
ptr = &b1;
ptr->print();
}