Sie sind auf Seite 1von 11

Attachments for ECE2036 Final Exam, Fall 2012

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

// Code for ECE3090 midterm, Constructors and Destructors question


class A {
public:
A();
// Default constructor
A(int);
// int Constructor
A(const A&); // Copy constructor
A();
// Destructor
A operator+(A rhs) const; // Addition operator
public:
int x;
// Single data member
};
A A::operator+(A rhs) const
{
A r(x + rhs.x);
return r;
}
class B {
public:
B();
// Default Constructor
B(int);
// int Constructor
B(const B&); // Copy constructor
B();
// Destructor
B operator+(const B& rhs) const; // Addition operator
public:
int x;
// Single data member
};
B B::operator+(const B& rhs) const
{
return B(x + rhs.x);
}
void Sub1(const A& a)
{
A a2(a + a);
}
void Sub2(B b)
{
B b2(b + b);
}
int main()
{
A a(1);
B b(2);
Sub1(a);
Sub2(b);
}

Program Constructors-Destructors.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

// Inheritance program
//ECE2036, Fall 2012
#include <iostream>
class Base {
public:
virtual void Func1()
void Func2()
virtual void Func3()
};

for ECE2036 final exam

const = 0;
const;
const;

class Sub1 : public Base {


public:
void Func1() const;
void Func2() const;
};

class Sub2 : public Base {


public:
void Func2() const;
void Func3() const;
};
class Sub3 : public Sub1 {
public:
void Func1() const;
void Func2() const;
};
class Sub4 : public Sub2 {
public:
void Func1() const;
};

Program inheritance.cc

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

// Implementations
// Base
void Base::Func2() const
{
std::cout << "Hello from
};
void Base::Func3() const
{
std::cout << "Hello from
};
// Sub1
void Sub1::Func1() const
{
std::cout << "Hello from
};
void Sub1::Func2() const
{
std::cout << "Hello from
};
// Sub2
void Sub2::Func2() const
{
std::cout << "Hello from
};
void Sub2::Func3() const
{
std::cout << "Hello from
};
// Sub3
void Sub3::Func1() const
{
std::cout << "Hello from
};
void Sub3::Func2() const
{
std::cout << "Hello from
};
// Sub4
void Sub4::Func1() const
{
std::cout << "Hello from
};

Base::Func2()" << std::endl;

Base::Func3()" << std::endl;

Sub1::Func1()" << std::endl;

Sub1::Func2()" << std::endl;

Sub2::Func2()" << std::endl;

Sub2::Func3()" << std::endl;

Sub3::Func1()" << std::endl;

Sub3::Func2()" << std::endl;

Sub4::Func1()" << std::endl;

Program inheritance.cc (continued)

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

// Functions for testing


void BaseRef(const Base& b)
{
b.Func2();
b.Func1();
}
void Sub1Val(Sub1 s1)
{
s1.Func1();
s1.Func2();
s1.Func3();
}
void Sub2Ref(const Sub2& s2)
{
s2.Func1();
s2.Func2();
s2.Func3();
}

int main()
{
Sub1 s1;
Sub3 s3;
Sub4 s4;
BaseRef(s1);
BaseRef(s3);
Sub1Val(s3);
Sub2Ref(s4);
}

Program inheritance.cc (continued)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

//
// ECE2036 Matrix and MatrixRow Class implementation
//
// All #include commands and namespace omitted for brevity.
//
typedef int Element t;
typedef unsigned int Index t;
class Matrix
{ // Matrix class declaration
public:
//Constructors;
Matrix();
// Constructs an empty 0x0 matrix with NaM
Matrix(Index t r, Index t c); // Construct with specified rows, columns
Matrix(const std::string&);
// Construct from string "(i,j,k..),(a,b,c..)"
Matrix(const Matrix&);// Copy constructor
Matrix();
// Destructor
// Operators
Matrix& operator=(const Matrix&);
Element t* operator[](int whichRow) const;
// Arithmetic operators
Matrix operator+(Matrix );
// Other operators omitted for brevity
public:
Index t nRows;
Index t nCols;
Element t* elements; // Points to the elements
bool
NaM;
// True if "Not a matrix".
};
Matrix::Matrix()
: nRows(0), nCols(0), NaM(true)
{ // Default constructor
// Nothing else needed
}
Matrix::Matrix(Index t r, Index t c)
: nRows(r), nCols(c), NaM(false)
{ // Construct with specified rowCount and colCount
elements = new Element t[nRows * nCols];
// Initialize to all zeros
for (Index t i = 0; i < nRows * nCols; ++i) elements[i] = 0;
}
Matrix::Matrix(const std::string& st)
{ // string constructor omitted for brevity. Assume it is correct
}
Matrix::Matrix(const Matrix& rhs)
: nRows(rhs.nRows), nCols(rhs.nCols), NaM(rhs.NaM)
{ // Copy constructor
elements = rhs.elements;
}

Program matrix.cc

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

Matrix::Matrix()
{
}
Matrix& Matrix::operator=(const Matrix& rhs)
{ // Assignment operator
nRows = rhs.nRows;
nCols = rhs.nCols;
NaM = rhs.NaM;
// Allocate and copy elements
elements = new Element t[nRows * nCols];
for (Index t i = 0; i < nRows * nCols; ++i)
{
elements[i] = rhs.elements[i];
}
return *this;
}
// Indexing operator
Element t* Matrix::operator[](int whichRow) const
{
return &elements[whichRow * nCols];
}

// Operators
Matrix Matrix::operator+(Matrix rhs)
{
Matrix ret(nRows, nCols); // Return matrix of correct shape
for (Index t r = 0; r < nRows; ++r)
{
for (Index t c = 0; c < nCols; ++c)
{
ret[r][c] = (*this)[r][c] + rhs[r][c];
}
}
return ret;
}
// Other operators omitted for brevity

Program matrix.cc (continued)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

// Demonstrate use of Templates to make a linked list.


// ECE2036 - Fall 2012
// Includes omitted for brevity
template <typename T> class List;
// Define a templated class that contains the users data
// and a "next" pointer.
template <typename T> class ListNode
{
public:
ListNode(const T& e) : next(0), element(e) {}
public:
ListNode<T>* next;
T
element; // The users data
};
template <typename T> class ListIterator
{ // Define an "iterator" to access list elements
public:
ListIterator() : current(0) {}
ListIterator(ListNode<T>* b) : current(b) {}
// Define not equal operator
bool operator !=(const ListIterator & rhs)
{
return current != rhs.current;
}
ListIterator operator++(int) // Postfix increment
{ // Postfix increment
ListIterator tmp(*this);
current = current->next;
return tmp;
}
ListIterator operator++()
// Prefix increment
{ // Prefix increment
current = current->next;
return *this;
}
T& operator*() const // Dereference operator
{
return current->element;
}
private:
ListNode<T>* current;
friend class List<T>;
};
// Now define the "List" class
template <typename T> class List {
public:

Program templatelinkedlist.cc

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

List() : head(0), tail(0) {}


void PushBack(const T& e)
{ // Add to end
ListNode<T>* n = new ListNode<T>(e);
if (!head) head = n;
else tail->next = n;
tail = n;
}
void PushFront(const T& e)
{ // Add to beginning
ListNode<T>* n = new ListNode<T>(e);
n->next = head;
if (!tail) tail = n;
head = n;
}

//
//
//
//

Make a new node


If list is empty, n is new head
Otherwise, old tail -> next is n
n is always new tail

//
//
//
//

Make a new node


New element next is old head
List was empty, n is new tail
n is always new head

void Insert(const ListIterator<T>& i, const T& e)


{ // Insert an element after specified iterator
ListNode<T>* n = new ListNode<T>(e);
n->next = i.current->next;
i.current->next = n;
if (i.current == tail) tail = n;
}
ListIterator<T> Begin()
{ // Return an iterator starting at the first element
return ListIterator<T>(head);
}
ListIterator<T> End()
{ // Return an iterator representing one beyond end
return ListIterator<T>(0);
}
private:
ListNode<T>* head;
ListNode<T>* tail;
};

Program templatelinkedlist.cc (continued)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

// Define a template subroutine to compute the sort a collection of elements


// specified by two iterators
#include <algorithm>
using namespace std;
template <class T>
void Sort(T b, T e)
{ // This sort is inefficient, and used for illustrative purposes only
while(b != e)
{
T i = b;
while(i != e)
{
if (*i < *b)
{ // Need to swap. This iter swap is defined in "algorithm"
iter swap(i, b); // Swap the two values
}
++i;
}
++b;
}
}
class A {
public:
A() : a(0) {};
A(int a0) : a(a0) {};
bool operator<(const A& rhs) { return a < rhs.a; } // Less-than operator
public:
int a;
};
class B
{
public:
B() : b(0) {};
B(int b0) : b(b0) {};
bool operator>(const B& rhs) { return b > rhs.b; } // Greater-than operator
public:
int b;
};
int main()
{
char s[] = "This is a test";
A a1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
B b1[10] = {0, 1, 7, 6, 5, 4, 6, 7, 8, 9};
// Now call the Sort routine with differing parameters
Sort(s, s + 14);
Sort(&a1[0], &a1[10]);
Sort(&b1[0], &b1[10]);
Sort(&a1[10], &b1[10]);
}

Program sort.cc

Program sort.cc (continued)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

// ECE2036 Final Exam


// Containers question
// (Includes and namespace omitted for brevity)
int main()
{
typedef map<char, int>
MyMap t;
typedef MyMap t::value type MyMapPair t;
vector<int> v1;
deque<int> d1;
MyMap t
m1;
// Populate the vector
for (int k = 0; k < 5; ++k)
{
v1.push back(k);
v1.push back(-k);
}
vector<int> v2(v1); // Copy construtor
// VECTOR PRINT HERE
cout << "v1 size " << v1.size() << " v2 size " << v2.size()
<< " v1 front() " << v1.front() << " v2.back() " << v2.back() << endl;
// END VECTOR PRINT
// Populate the deque
for (int k = 0; k < 5; ++k)
{
d1.push back(k);
d1.push front(-k);
}
// DEQUE PRINT HERE
cout << "d1.front() " << d1.front() << " d1.back() " << d1.back()
<< " d1[5] " << d1[5] << endl;
// END DEQUE PRINT
// Populate the map
m1.insert(MyMapPair t(B, 3));
m1.insert(MyMapPair t(A, 4));
m1.insert(MyMapPair t(D, 1));
m1.insert(MyMapPair t(A, 1));
m1[K] = 10;
// MAP PRINT HERE
cout << "m1.size() " << m1.size()
<< " begin.first " << m1.begin()->first
<< " m1.begin.second " << m1.begin()->second << endl
<< " --(m1.end).first " << (--m1.end())->first
<< " --(m1.end).second " << (--m1.end())->second << endl;
// END MAP PRINT
}

Program Containers.cc

10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

// Static Members attachment


// ECE2036 Final Exam
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() : a(0), b(0) { } // Constructor
// member variables
int a;
int b;
// member functions
void Func1();
void Func2();
};
// Member function implementations
void MyClass::Func1()
{
cout << "Hello from MyClass::Func1()" << endl;
}
void MyClass::Func2()
{
cout << "Sum of a and b is " << a + b << endl;
}
int main()
{
}

Program Static.cc

11

Das könnte Ihnen auch gefallen