Download presentation
Presentation is loading. Please wait.
1
CS1220 Midterm Review
2
In the class definition for Book below, clearly mark which lines are constructors (C),
which are accessors (A), and which are mutators (M). class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
3
In the class definition for Book below, clearly mark which lines are attributes (A).
class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
4
In the class definition for Book below there is no destructor, what other
functions are missing (i.e., we probably should not use the default implementations)? class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
5
Write the function signature for the copy constructor for the class definition
for Book. class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
6
Write the function signature for the assignment operator for the class definition
for Book. class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
7
Write the function signature for the destructor for the class definition for Book.
class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
8
In the function getNumPages() in class definition for Book below, what does the
keyword “const” do? class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
9
Suppose the constructor for Book appears as written
Suppose the constructor for Book appears as written. Write the corresponding destructor? class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; author = new char[len]; strcpy(author, newAuthor.c_str()); } private: char *author; int numPages; };
10
What is the purpose of the bolded section of the constructor below
What is the purpose of the bolded section of the constructor below? What is this section called? class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; author = new char[len]; strcpy(author, newAuthor.c_str()); } private: char *author; int numPages; };
11
Suppose the constructor for Book appears as written
Suppose the constructor for Book appears as written. Write the corresponding copy constructor? class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; author = new char[len]; strcpy(author, newAuthor.c_str()); } private: char *author; int numPages; };
12
In the class declaration below, what does the bolded code do? What
is it called? class Book { public: Book (string newAuthor, int pages = 1); private: char *author; int numPages; };
13
Given the constructor below, write down examples of the three
ways in which a Book might be initialized when declared? class Book { public: Book (string newAuthor = “”, int pages = 1); private: char *author; int numPages; };
14
Suppose we would like to combine two Books using the ‘+’
operator. Write a function signature for this operator as a friend of the class Book? class Book { public: Book (string newAuthor = “”, int pages = 1); private: char *author; int numPages; };
15
In the following code found at the beginning of a header file,
what is the purpose of these statements? #ifndef BOOK_H #define BOOK_H …. The rest of the header file #endif
16
What is the English description of the following declaration?
int (*maxFunction) (int left, int right);
17
Identify the 4 bugs in the following class declaration, and annotate why they
are bugs. class Tree { public: int Tree (string name) : name(treeName) {} int grow(); string getName(); void setName(string name) : treeName(name) {} private: string treeName; }
18
Given the following node declaration and a singly-linked list as
shown below, write code which allocates a new node, sets its key value to 9, and inserts the node into the list in sorted order. Declare any pointer objects you deem necessary to accomplish the task. Your code must only handle this specific insertion case, i.e., it need not handle general insertion cases. class Node { public: int key; node *next; }; head 4 7 12
19
Write a program called add that takes two integers from the command line
and adds those two integers and prints the results (see the example execution below). The program should report an error, if only one number is supplied or more than two number are supplied. john> add 23 21 The result is 44 john> add 16 Error: you must supply exactly two numbers john> add
20
Write a program which creates a vector of integers, and inserts ten
random integers into the vector.
21
In the class definition for Book below, clearly mark which lines are constructors (C),
which are accessors (A), and which are mutators (M). class Book { public: C Book (); C Book (string newAuthor, int newNumPages); void read (int pageNum); A string getAuthor() const {return author;} A int getNumPages() const {return numPages;} M void setAuthor(string newAuthor) {author = newAuthor;} M void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
22
In the class definition for Book below, clearly mark which lines are attributes (A).
class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: A char *author; A int numPages; };
23
In the class definition for Book below there is no destructor, what other
functions are missing (i.e., we probably should not use the default implementations)? Copy Constructor, Assignment Operator class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
24
Write the function signature for the copy constructor for the class definition
for Book. Book& Book(const Book &b); class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
25
Write the function signature for the assignment operator for the class definition
for Book. Book& operator=(const Book &b); class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
26
Write the function signature for the destructor for the class definition for Book.
class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
27
In the function getNumPages() in class definition for Book below, what does the
keyword “const” do? Ensures the Book referenced within the function by this remains unchanged. class Book { public: Book (); Book (string newAuthor, int newNumPages); void read (int pageNum); string getAuthor() const {return author;} int getNumPages() const {return numPages;} void setAuthor(string newAuthor) {author = newAuthor;} void setNumPages(int newNumPages) {numPages = newNumPages;} private: char *author; int numPages; };
28
Suppose the constructor for Book appears as written
Suppose the constructor for Book appears as written. Write the corresponding destructor? Book::~Book() { delete [] author; } class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; author = new char[len]; strcpy(author, newAuthor.c_str()); private: char *author; int numPages; };
29
What is the purpose of the bolded section of the constructor below
What is the purpose of the bolded section of the constructor below? What is this section called? Purpose: to initialize data member(s) of the class. Its name: base init. section. class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; author = new char[len]; strcpy(author, newAuthor.c_str()); } private: char *author; int numPages; };
30
Suppose the constructor for Book appears as written
Suppose the constructor for Book appears as written. Write the corresponding copy constructor? Book::Book(const Book &b) numPages(b.numPages) { int len = strlen(b.author) + 1; author = new char[len]; strcpy(author, b.author); } class Book { public: Book (string newAuthor, int pages) : numPages(pages) { int len = newAuthor.size() + 1; strcpy(author, newAuthor.c_str()); private: char *author; int numPages; };
31
In the class declaration below, what does the bolded code do? What
is it called? Do: provide a value to pages, if none is provided in parameter list. Called: default parameter class Book { public: Book (string newAuthor, int pages = 1); private: char *author; int numPages; };
32
Given the constructor below, write down examples of the three
ways in which a Book might be initialized when declared? Book b1, b2(“Shomper”), b3(“Shomper”, 50); class Book { public: Book (string newAuthor = “”, int pages = 1); private: char *author; int numPages; };
33
Suppose we would like to combine two Books using the ‘+’
operator. Write a function signature for this operator as a friend of the class Book? friend Book operator+(const Book &b1, const Book &b2); class Book { public: Book (string newAuthor = “”, int pages = 1); private: char *author; int numPages; };
34
In the following code found at the beginning of a header file,
what is the purpose of these statements? Prevents multiple inclusions of the header file. #ifndef BOOK_H #define BOOK_H …. The rest of the header file #endif
35
What is the English description of the following declaration?
maxFunction is a pointer to a function that takes two integer parameters and returns an integer. int (*maxFunction) (int left, int right);
36
Identify the 4 bugs in the following class declaration, and annotate why they
are bugs. Bugs denoted in bold. Five actual bugs. class Tree { public: int Tree (string name) : name(treeName) {} // no return in ctor // init is reversed int grow(); string getName(); // getters should const *this void setName(string name) : treeName(name) {} // only ctor has // base init. section private: string treeName; } // no trailing semi-colen for class
37
Given the following node declaration and a singly-linked list as
shown below, write code which allocates a new node, sets its key value to 9, and inserts the node into the list in sorted order. Declare any pointer objects you deem necessary to accomplish the task. Your code must only handle this specific insertion case, i.e., it need not handle general insertion cases. Node *prev = head->next; Node *post = prev->next; Node *curr = new Node; curr->key = 9; curr->next = post; crev->next = curr; class Node { public: int key; node *next; }; curr 9 head 4 7 12 prev post
38
Write a program called add that takes two integers from the command line
and adds those two integers and prints the results (see the example execution below). The program should report an error, if only one number is supplied or more than two number are supplied. Program supplied at ~shomperk/CS1220/public/Exams john> add 23 21 The result is 44 john> add 16 Error: you must supply exactly two numbers john> add
39
Write a program which creates a vector of integers, and inserts ten
random integers into the vector. Program supplied at ~shomperk/CS1220/public/Exams
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.