Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



Topics covered in the Final of CS 135

•    About 15-20% contents before mid-term will be tested again in the final. For example, how to use branch (including switch statement) and repetition statement.

•     Define and use methods.

1.   Method overload: same name, similar functionality, different signatures (parameter list is different in number, type, or order). The return type for overloaded methods   do not need to be exactly the same.

Note that method override is different from method overload. Method override        means when a subclass inherits a method from superclass, and if the method in the subclass behaves different from the method in the superclass, then we will override that method in subclass. When method overrides, method name, return type,            parameter list all need to be matched to the corresponding method in superclass.

2.   A common mistake is trying to print intermediate result in a method. Another             common mistake is not to return a value of the corresponding type in every possible situation.

For example,

double getMilesPerGallon() { //numMiles and numGallons are data members. if (numGallons != 0)

return (double)numMiles / numGallons;

}

The problem for the above code is, what is the return if numGallons is zero?

3.   Pay attention to the difference between == and =. Equality comparison operator ==    is to compare the left hand side expression equals to the right hand side expression   or not, if they are equal, return true, otherwise, return false. Neither side changes its value is not changed.

Assignment = operator runs from right to left. First evaluate the right-hand side expression. Second use the value to set the left-side variable. Third the whole    assignment expression has the value of the right-hand side expression.

Note that in C/C++, value 0 is false and 1 means true.

Look at the following example,

#include <iostream>

using namespace std;

int main()

{

//int i = 2.6; //Get a warning when compiled,

//cannot put 2.6 to int variable

//double d = 2; //work, put an int into a double type

int i = 2;


if (i = 3) //i is changed to be 3 by i = 3.

//So expression (i = 3) is 3.

//In C++, non-zero number means true.

//Run if-body and print i’s value is 3.

//The condition works in C++ syntax (but not in Java)

//but not fit for logic.

cout << "i's value is " << i << endl;

//output i's value is 3

string greeting = "hello";

string str = "hi";

if (str == greeting)

cout << str << " equals " << greeting << endl;

else cout << str << " does not equal " << greeting << endl;

//output

//hi does not equal hello

 

///if (str = greeting) //not compiled.

//a string cannot be a condition.

///   cout << "set " << str << " to be "

///       << greeting << endl;

 

return 0;

}

 

 

•     One-dimensional and two-dimensional array: a collection of same type elements put in adjacent positions

1.   Given an array, how to find out the number of elements in this array?

2.   How to access an individual element in an array?

3.   How to find out sum, average, max, min, and other aggregate operations of array? How to find out whether an element is contained inside an array or not?

•    Vector

1.   Vector is like a smart array, with appropriate operations for array provided. You can also think it is an array that can change size. A vector can grow from initial                   compacity. Know how to define a vector of a given type.

2.   Know how to initialize a vector.

3.   Use size and push_back of vector, and access an element of vector using index notation.

•     Pointer

1.   Pointer stores the address of a variable. Use pointer for dynamically allocated array.

2.   Dereference operator * is followed by a pointer. Suppose p is a properly declared and initialized int pointer. Then *p is the int residing in the address saved in p.


3.   Arrays and Pointers

int* arr; is the same as int arr[];

(arr+1) is the address of arr[1].

*(arr+1) is the same as arr[1].

•     Define class (encapsulation) and use its objects. These classes may include arrays as data members.

1.   What are class and object?

2.   What is encapsulation?

3.   Given a class, how to write setters and getters?

4.   Given a class, how to write constructors?

5.   Is there a return type for constructor? Must constructor share the same name as class?

6.   Given constructors of a class, how to create an object from that class?

7.   What are the unique things to pay attention to when data members are arrays (can be one-dimensional arrays). In constructors/setters, make an independent copy of   the parameter to the data member. In getters, return an independent copy of the    data member.

8.   Define a class.

•    Recursion: a function calls itself directly or indirectly. Need to know how to define and/or read recursion codes.

•     Inheritance

1.   What are superclass and subclass? Every object of a subclass is an object of the corresponding superclass. For example, every student is a person.

2.   Given the following:

Person* p = new Person(“Ann”);

Student* s = new Student(“Alice”);

Is p = s; allowed?

//Yes. A person can be set by a student.

//It is like a student can reside in room labelled as Person.

//It is like a small foot put in a big shoe.

Is s = p; allowed?

//No. A student cannot be set by a person.

//It is like a person cannot reside in a room labelled as Student.

//It is like to squeeze a big foot in a small shoe.

Similarly, suppose ChoiceQuestion is a subclass of Question.

Question q1;

ChoiceQuestion q2;

q1 = q2; //allowed


q2 = q1; //not allowed

•     Polymorphism (will not be tested in final of Fall 2021).

With polymorphism, codes written for super class can apply to any subclass objects. Here are steps to achieve polymorphism.

(1)  Codes use pointers to superclasses.

(2)  Methods in superclass and subclass to be overridden are declared as virtual.