(100 points)
- (20 points)
What will be printed out when the application
a_01.cpp is executed?
- (20 points)
Rewrite the given test function to test the full membership
in every level of hierarchy.
- (30 points)
Rewrite the given class using stl type
vector of int items.
Write down, both interface and implementation
of class A.
- (30 points)
Rewrite the updated class as interface and implemented
class either in Java or C#.
// a_01.cpp
#include "A.h"
#include "B.h"
#include "C.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
A* a_0 = new C;
cout << "0 -" << endl;
a_0->print();
cout << endl;
cout << "1 -" << endl;
a_0->print();
cout << endl;
a_0 = new B(7,3);
cout << "2 -" << endl;
a_0->print();
cout << endl;
a_0 = new C(6);
cout << "3 -" << endl;
a_0->print();
cout << endl;
}
// A.h
#ifndef A_H
#define A_H
class A {
public:
A(const int a=2,
const int b=4,
const int c=4);
A(const A&);
A& operator=(const A&);
~A();
virtual int get_p() const;
virtual void set_p(const int);
virtual int get_q() const;
virtual void set_q(const int);
virtual int get_r() const;
virtual void set_r(const int);
virtual void print();
protected:
int p,
q,
r;
};
#endif
// A.cpp
#include "A.h"
#include <iostream>
using namespace std;
A::
A(const int a,
const int b,
const int c) : p(a),
q(b),
r(c) {
cout << ".A." << endl;
}
A::
A(const A& o) : p(o.p),
q(o.q),
r(o.r) {
}
A&
A::
operator=(const A& o) {
if(this==&o) return *this;
p = o.p;
q = o.q;
r = o.r;
return *this;
}
A::
~A() {
cout << "-A-" << endl;
}
int
A::
get_p() const {
return p;
}
void
A::
set_p(const int a) {
p = a;
}
int
A::
get_q() const {
return q;
}
void
A::
set_q(const int a) {
q = a;
}
int
A::
get_r() const {
return r;
}
void
A::
set_r(const int a) {
r = a;
}
void
A::
print() {
cout << p << ", " << q << ", " << endl;
}
// B.h
#ifndef B_H
#define B_H
#include "A.h"
class B : public A {
public:
B(const int a=2,
const int b=1,
const int c=1);
B(const B&);
B& operator=(const B&);
~B();
virtual void set_p_q_r(const int);
virtual void print();
};
#endif
// B.cpp
#include "B.h"
#include <iostream>
using namespace std;
B::
B(const int a,
const int b,
const int c) : A(a,b,c) {
cout << "..B.." << endl;
}
B::
B(const B& o) {
A::p = o.p;
A::q = o.q;
A::r = o.r;
}
B&
B::
operator=(const B& o) {
if(this==&o) return *this;
A::p = o.p;
A::q = o.q;
A::r = o.r;
return *this;
}
B::
~B() {
cout << "--B--" << endl;
}
void
B::
set_p_q_r(const int a) {
A::p = a;
A::q = a;
A::r = a;
}
void
B::
print() {
cout << endl;
cout << A::p << endl;
cout << A::q << endl;
cout << A::r << endl;
cout << endl;
}
// C.h
#ifndef C_H
#define C_H
#include "B.h"
class C : public B {
public:
C(const int a=2);
C(const C&);
C& operator=(const C&);
~C();
virtual void set_p_q_r(const int,
const int,
const int);
};
#endif
// C.cpp
#include "C.h"
#include <iostream>
using namespace std;
C::
C(const int a) : B(a,a,a) {
cout << "...C..." << endl;
}
C::
C(const C& o) {
A::p = o.p;
A::q = o.q;
A::q = o.q;
}
C&
C::
operator=(const C& o) {
if(this==&o) return *this;
A::p = o.p;
A::q = o.q;
A::r = o.r;
return *this;
}
C::
~C() {
cout << "---C---" << endl;
}
void
C::
set_p_q_r(const int a,
const int b,
const int c) {
A::p = a;
A::q = b;
A::r = c;
}
.A.
..B..
...C...
0 -
2
2
2
1 -
2
2
2
.A.
..B..
2 -
7
3
1
.A.
..B..
...C...
3 -
6
6
6