Hunter Liu's Website

18. Week 10 Tuesday: Practise with Classes and Pointers

≪ 17. Week 9 Tuesday: Classes and the Private Keyword | Table of Contents | 19. Week 10 Thursday: Pointers and C-Style Arrays ≫

In preparation for the final next week, I’ve put together yet another collection of practise problems containing find the error and predict the output type questions. Coding problems involving these concepts aren’t particularly productive, as tasks or scenarios demanding a substantial application of these concepts are typically too difficult to squeeze into a reasonable time frame (particularly in an exam setting), plus you can find these throughout previous weeks’ notes.

Find the Error

As usual, your job is to find the error(s) in the following snippets of code and classify them as runtime or compile errors. As a fun fact, several of these are based on errors I’ve made when programming some of my own projects. Hopefully, seeing these in a sterilised setting will help you avoid similar mistakes in the future…

Problem 1.

 1#include <iostream> 
 2#include <string>
 3#include <vector> 
 4
 5using namespace std; 
 6
 7class classroom {
 8public: 
 9    void add_student(string s) {
10        students.push_back(s); 
11    } 
12
13private: 
14    vector<string> students; 
15};
16
17int main() {
18    classroom c; 
19    c.add_student("George"); 
20    c.add_student("Elizabeth"); 
21    c.add_student("Thomas"); 
22    c.add_student("Frank"); 
23    c.add_student("Ulysses"); 
24
25    cout << c.students.size() << endl; 
26    return 0; 
27} 

Problem 2.

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5class foo {
 6    int a; 
 7
 8    foo() : a(0) {} 
 9    foo(int _a) : a(_a) {} 
10}; 
11
12int main() {
13    foo f1; 
14    foo f2(37); 
15    return 0; 
16} 

Problem 3.

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5int* make_ptr() {
 6    int i = 5; 
 7    return &i; 
 8} 
 9
10int main() {
11    int* ptr = make_ptr(); 
12    cout << *ptr << endl; 
13    return 0; 
14} 

Problem 4.

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5class cat {
 6public: 
 7    cat(string _name) : name(_name), meows(0) {} 
 8
 9    void meow() const {
10        cout << name << " says meoww..." << endl; 
11        meows++; 
12    } 
13
14    void show_meows() const {
15        cout << name << " has meowed " 
16             << meows  << " time(s)." << endl; 
17    }
18
19private: 
20    string name; 
21    int meows; 
22};  
23
24int main() {
25    cat c("Harry"); 
26    c.meow(); 
27    c.meow(); 
28    c.meow(); 
29    c.show_meows(); 
30
31    return 0; 
32} 

Challenge 5.

 1struct foo {
 2    int a; 
 3    foo(int _a) : a(_a) {} 
 4}; 
 5
 6struct bar {
 7    foo f; 
 8    int b; 
 9    bar(foo _f, int _b) {
10        b = _b; 
11        f = _f; 
12    } 
13}; 
14
15int main() {
16    return 0; 
17} 

Predict the Output

You may assume that none of these have any errors and will run on any machine!

Problem 6.

 1#include <iostream> 
 2#include <vector>
 3
 4using namespace std; 
 5
 6class sequence {
 7public: 
 8    // default constructor 
 9    sequence(); 
10
11    int compute(int n); 
12
13private: 
14    vector<int> terms; 
15}; 
16
17int main() {
18    sequence f; 
19    cout << f.compute(7) << endl; 
20    cout << f.compute(5) << endl; 
21    cout << f.compute(3) << endl; 
22    cout << f.compute(9) << endl; 
23    
24    return 0; 
25} 
26
27sequence::sequence() {
28    // set up the first two terms of the sequence 
29    terms.push_back(1); 
30    terms.push_back(1); 
31} 
32
33int sequence::compute(int n) {
34    while(terms.size() < n) {
35        terms.push_back(terms.at(terms.size() - 1)); 
36        terms.at(terms.size() - 1) += terms.at(terms.size() - 3); 
37    } 
38
39    return terms.at(n - 1); 
40} 

Problem 7.

 1#include <iostream>
 2#include <string>
 3
 4using namespace std;
 5
 6int main() {
 7    string s1 = "abcd";
 8    string s2 = "efgh";
 9
10    string* p1 = &s1;
11    string* p2 = &s2;
12    string* p3 = p1;
13
14    for(int i = 0; i < s2.size(); i++) {
15        if(p1->at(i) == 'c') {
16            p2->push_back('d');
17            p1->push_back('a');
18        }
19
20        p3->push_back('c');
21    }
22    
23    cout << s1 << endl;
24    cout << s2 << endl;
25
26    return 0;
27} 

Challenge 8. Constructor Mayhem

 1#include <iostream> 
 2
 3using namespace std; 
 4
 5class child {
 6public: 
 7    child() {
 8        cout << "Nameless Child Spawned" << endl; 
 9    } 
10
11    child(string _name) : name(_name) {
12        cout << name << " Spawned" << endl; 
13    } 
14
15private: 
16    string name; 
17}; 
18
19class parent {
20public: 
21    // empty default constructor 
22    parent() {} 
23
24    // two different ways to set the contents of c. 
25    parent(child _c) : c(_c) {} 
26    parent(child _c, int i) {
27        c = _c; 
28    } 
29
30private: 
31    child c; 
32}; 
33
34int main() {
35    child c1("George"); 
36    child c2; 
37    child c3 = c2; 
38
39    parent p1; 
40    parent p2(c2); 
41    parent p3(c3, 0); 
42
43    return 0; 
44}