(60 points)
- (10 points)
What will be printed out when the application
a_01.cpp is executed?
- (20 points)
Rewrite the given class using stl type
string of two char items.
Write down, both interface and implementation
of class A.
- (10 points)
Write a member function that accesses length of
string.
Write down, both interface and implementation
of class A.
- (20 points)
Rewrite the updated class as interface and implemented
class either in Java or C#.
// a_01.cpp
#include <iostream>
#include "A.h"
using namespace std;
int main(int argc, char* argv[]) {
A a0('s',
'y');
cout << "0 -";
cout << endl;
cout << a0;
cout << endl;
a0.set_c1('p');
A a1 = a0;
cout << "1 -";
cout << endl;
cout << a1;
cout << endl;
a1.set_c1_c2('l',
'y');
A a2(a0);
cout << "2 -";
cout << endl;
cout << a2;
cout << endl;
a2.set_c1_c2('e',
'k');
cout << "3 -";
cout << endl;
cout << a2.get_c1();
cout << endl;
cout << a2.get_c2();
cout << endl;
}
// A.h
#ifndef A_H
#define A_H
#include <iostream>
using namespace std;
class A {
public:
A(const char,
const char);
A(const A&);
A& operator=(const A&);
char get_c1() const;
void set_c1(const char);
char get_c2() const;
void set_c2(const char);
void set_c1_c2(const char);
void set_c1_c2(const char,
const char);
friend ostream& operator<<(ostream&,
const A&);
private:
char c1;
char c2;
};
#endif
// A.cpp
#include "A.h"
A::
A(const char a,
const char b) : c1(a),
c2(b) {
}
A::
A(const A& o) : c1(o.c1),
c2(o.c2) {
}
A&
A::
operator=(const A& o) {
if(this==&o) return *this;
c1 = o.c1;
c2 = o.c2;
return *this;
}
char
A::
get_c1() const {
return c1;
}
char
A::
get_c2() const {
return c2;
}
void
A::
set_c1(const char a) {
c1 = a;
}
void
A::
set_c2(const char a) {
c2 = a;
}
void
A::
set_c1_c2(const char a) {
c1 = a;
c2 = a;
}
void
A::
set_c1_c2(const char a,
const char b) {
c1 = a;
c2 = b;
}
ostream&
operator<<(ostream& os,
const A& o) {
os << o.c1 << endl;
os << o.c2 << endl;
return os;
}
0 -
s
y
1 -
p
y
2 -
p
y
3 -
e
k
(40 points)
- (10 points)
What will be printed out when the application
a_02.cpp is executed?
- (30 points)
Rewrite the given family of classes and the test function
either in Java or C#.
// a_02.cpp
#include <iostream>
using namespace std;
#include "A.h"
#include "B.h"
int main(int argc, char* argv[]) {
A* a1 = new B(3,
4);
cout << "a1 -";
cout << endl;
a1->print();
a1->set_d(5,
6);
cout << "a1 -";
cout << endl;
a1->print();
delete a1;
}
// A.h
#ifndef A_H
#define A_H
#include <iostream>
using namespace std;
class A {
public:
A(const int,
const int);
A(const A&);
virtual ~A();
virtual A& operator=(const A&);
virtual int get_s() const;
virtual int* get_d() const;
virtual void set_d(const int, // index
const int); // value
virtual void print() const;
protected:
int s;
int* d;
};
#endif
// A.cpp
#include "A.h"
A::
A(const int a,
const int v) : s(a),
d(new int[s]) {
for(int i=0; i<s; i++)
d[i] = v;
cout << "-cA-" << endl;
}
A::
A(const A& o) : s(o.s),
d(new int[s]) {
for(int i=0; i<s; i++)
d[i] = o.d[i];
}
A::
~A() {
delete [] d;
cout << "-Ad-" << endl;
}
A&
A::
operator=(const A& o) {
if(this==&o) return *this;
delete [] d;
s = o.s;
d = new int[s];
for(int i=0; i<s; i++)
d[i] = o.d[i];
return *this;
}
int
A::
get_s() const {
return s;
}
int*
A::
get_d() const {
return d;
}
void
A::
set_d(const int k,
const int v) {
delete [] d;
s = k;
d = new int[s];
for(int i=0; i<s; i++)
d[i] = v;
}
void
A::
print() const {
cout << 'A' << endl;
for(int i=0; i<s; i++)
cout << d[i] << ' ';
cout << endl;
}
// B.h
#ifndef B_H
#define B_H
#include <iostream>
using namespace std;
#include "A.h"
class B : public A {
public:
B(const int,
const int);
B(const B&);
virtual ~B();
virtual B& operator=(const B&);
virtual void print() const;
};
#endif
// B.cpp
#include "B.h"
B::
B(const int a,
const int b) : A(a,b) {
cout << "--cB--" << endl;
}
B::
B(const B& o) : A(o) {
}
B::
~B() {
delete [] A::d;
cout << "--Bd--" << endl;
}
B&
B::
operator=(const B& o) {
if(this==&o) return *this;
delete [] d;
s = o.s;
d = new int[s];
for(int i=0; i<s; i++)
d[i] = o.d[i];
return *this;
}
void
B::
print() const {
cout << 'B' << " /"<< endl;
for(int i=0; i<s; i++)
cout << d[i] << endl;
cout << endl;
}
-cA-
--cB--
a1 -
B /
4
4
4
a1 -
B /
6
6
6
6
6
--Bd--
-Ad-