c++ training datascope lawrence d’antonio lecture 1 quiz 1

33
C++ Training C++ Training Datascope Datascope Lawrence D’Antonio Lawrence D’Antonio Lecture 1 Lecture 1 Quiz 1 Quiz 1

Post on 21-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

C++ TrainingC++ TrainingDatascopeDatascope

Lawrence D’AntonioLawrence D’Antonio

Lecture 1Lecture 1

Quiz 1Quiz 1

Page 2: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

Quiz 1Quiz 1

(1) What Danish computer scientist (1) What Danish computer scientist invented C++?invented C++?

Bjarne StroustrupBjarne Stroustrup

(2) What was the first object-oriented (2) What was the first object-oriented programming language? programming language?

SimulaSimula

Page 3: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(3) What does STL stand for and who (3) What does STL stand for and who invented it?invented it?

Standard Template Library, Alex StepanovStandard Template Library, Alex Stepanov

(4) (a) Which language is older: C++ or (4) (a) Which language is older: C++ or Java? Java?

C++ (sort of)C++ (sort of) (b) Which language is older: C++ or (b) Which language is older: C++ or

Smalltalk?Smalltalk? SmalltalkSmalltalk

Page 4: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(5) True or false: C++ was designed to be (5) True or false: C++ was designed to be backwards compatible with C.backwards compatible with C.

True (a least in theory, in practice is True (a least in theory, in practice is another story) another story)

(6) What is the difference between (6) What is the difference between overloading and polymorphism?overloading and polymorphism?

Overloading creates many functions of the Overloading creates many functions of the same name that work on different types. same name that work on different types. Polymorphism creates one function that Polymorphism creates one function that work on different types. work on different types.

Page 5: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(7) True or false: Only object-oriented (7) True or false: Only object-oriented programming languages can exhibit programming languages can exhibit polymorphism.polymorphism.

False, many languages exhibit False, many languages exhibit polymorphism. In Lisp, car and cons are polymorphism. In Lisp, car and cons are examples of polymorphic functions. examples of polymorphic functions.

Page 6: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(8) What is the difference between (8) What is the difference between function overloading and function function overloading and function overriding?overriding?

Overloading involves functions of the Overloading involves functions of the same name having different signatures. same name having different signatures. Overriding occurs in when a function in a Overriding occurs in when a function in a derived class replaces a function of the derived class replaces a function of the same name in the base class. same name in the base class.

Page 7: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(9) What is a function object?(9) What is a function object? A function object is an object that may be A function object is an object that may be

called like a function. In C++ this is called like a function. In C++ this is achieved by overloading the () operator.achieved by overloading the () operator.

(10) How does a function object differ from (10) How does a function object differ from a function?a function?

A function object has state. It can be A function object has state. It can be initialized through a constructor and initialized through a constructor and retains its state between invocations. retains its state between invocations.

Page 8: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(11) Which of the following C++ mechanisms is (11) Which of the following C++ mechanisms is used to create polymorphism?used to create polymorphism?

Virtual functions X Templates XVirtual functions X Templates X

Virtual functions exemplify Virtual functions exemplify inclusion inclusion polymorphism.polymorphism. A reference or pointer to a A reference or pointer to a parent object may be converted to an object of parent object may be converted to an object of any inherited type. This means that determining any inherited type. This means that determining which method is being called is a run-time which method is being called is a run-time decision.decision.

Templates implement the concept of Templates implement the concept of parametric parametric polymorphism.polymorphism. Code works correctly for a Code works correctly for a variety of datatypes. Functional languages such variety of datatypes. Functional languages such as ML, Haskell, Lisp typically use parametric as ML, Haskell, Lisp typically use parametric polymorphism.polymorphism.

Page 9: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(12) What is a predicate in C++?(12) What is a predicate in C++? A function or function object that returns a A function or function object that returns a

boolean.boolean.

(13) What is the primary difference (13) What is the primary difference between a struct and a class in C++?between a struct and a class in C++?

In a struct, access is public by default In a struct, access is public by default whereas in a class it is private by default.whereas in a class it is private by default.

Page 10: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(14) What is the meaning of the key word (14) What is the meaning of the key word mutablemutable??

A data member of a class is declared to be A data member of a class is declared to be mutable then it may be modified inside a mutable then it may be modified inside a const member function. const member function.

Page 11: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

Mutable exampleMutable example

Ex:class Image {private:

mutable bool is_loaded;public:

void Redraw() const {if (is_loaded == false) {

//…load image from diskis_loaded = true

}//..paint image on screen

}};

Page 12: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(15) What are the member functions that (15) What are the member functions that the compiler will write for you?the compiler will write for you?

Default constructor, copy constructor, Default constructor, copy constructor, assignment operator, destructorassignment operator, destructor

(16) True or false: a default constructor (16) True or false: a default constructor cannot take any argumentscannot take any arguments

False. A default constructor must either False. A default constructor must either have no arguments or all arguments must have no arguments or all arguments must have default values have default values

Page 13: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(17) What are the four cast operators in (17) What are the four cast operators in C++?C++?

static_cast, dynamic_cast, static_cast, dynamic_cast, reinterpret_cast, const_cast reinterpret_cast, const_cast

Page 14: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(18) Which of the following conversions is (18) Which of the following conversions is generally not legal?generally not legal?

(a) Converting a base object to a derived (a) Converting a base object to a derived object?object?

Not legal (the base object is not a derived Not legal (the base object is not a derived object).object).

(b) Converting a derived object to a base (b) Converting a derived object to a base object?object?

Legal (the derived object is a base object).Legal (the derived object is a base object).

Page 15: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(19) What is an exception specification?(19) What is an exception specification? A specification of what types of exceptions A specification of what types of exceptions

a function may throw (or none at all).a function may throw (or none at all).

void f() throw(int); //throws only an intvoid g() throw(); //throws no exceptions

Page 16: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(20) What is a nothrow function and how (20) What is a nothrow function and how do you declare it?do you declare it?

A nothrow function “guarantees” that it A nothrow function “guarantees” that it won’t throw an exception. The only won’t throw an exception. The only example currently allowed is the nothrow example currently allowed is the nothrow new operator.new operator.

Page 17: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

nothrow codenothrow code

Class X {//…};

X *p = new (nothrow) X;

Note: even though this code “won’t” throw an exception, one can still check if the allocation was successful by using the code:

if (!p) {//…}

Page 18: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(21) True or false: it is illegal for a (21) True or false: it is illegal for a destructor to throw an exception.destructor to throw an exception.

False, but it is generally a bad idea. False, but it is generally a bad idea.

Page 19: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(22) How do you declare and allocate (22) How do you declare and allocate memory for a two dimensional array A with memory for a two dimensional array A with nn rows and rows and mm columns (where columns (where nn,,mm are are integer variables determined at run-time)?integer variables determined at run-time)?

int **A;

A = new *int[n];

for(int i = 0; i < n; i++)A[i] = new int[m];

Page 20: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(23) What is the most significant difference (23) What is the most significant difference between malloc() (the C dynamic memory between malloc() (the C dynamic memory allocation function) and new() (the C++ allocation function) and new() (the C++ dynamic memory allocation operator)?dynamic memory allocation operator)?

malloc() doesn’t call a constructor for malloc() doesn’t call a constructor for allocated objects, while new() does.allocated objects, while new() does.

Page 21: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(24) (a) Is the following code legal?(24) (a) Is the following code legal?

class X {};

class Y: public X{};

main(){

X *p = new Y;}

Legal, since a Y is an X.

Page 22: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(b) Is the following code legal?(b) Is the following code legal?

class X {};

class Y: private X{};

main(){

X *p = new Y;}

Illegal, since private inheritance is used, Y is not an X.

Page 23: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(25) What is a smart pointer?(25) What is a smart pointer?

A smart pointer is an object that acts like a A smart pointer is an object that acts like a pointer. A smart pointer can have state, so pointer. A smart pointer can have state, so it can be responsible for owning what it it can be responsible for owning what it points to. points to. auto_ptrauto_ptr is an example of a is an example of a smart pointer in the standard library.smart pointer in the standard library.

Page 24: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(26) What is wrong with this code?(26) What is wrong with this code?

int *p = new int[10];//do stuff with pdelete p;

In order to delete an array, one must use the syntax delete [] p;

Page 25: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(27) What is wrong with this code?(27) What is wrong with this code?

class X{private:

int a;public:

X(int n): a(n) {}};

main(){

X *p = new X;}

new X calls the default constructor for X, but the class does not have any default constructor defined.

Page 26: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(28) (a) What is wrong with the following (28) (a) What is wrong with the following declaration?declaration?

class X {public:

int x;X(int a) x(a) {}void f(int a) const;

};main(){

X my_x(5);int *p = &my_x::x;*p = 8;return 0;

}p is an int pointer, but X::x is an int inside class X. One needs to declare and use p like this:int X::*p = &X::x;my_x.*p = 8;

Page 27: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(30) What is wrong with the following (30) What is wrong with the following function?function?

int foo(const vector<int> &v){

n = v.size();if (n = 0) { //Check if vector is empty

cout << “Error: Empty vector\n”;return -1;

}return v[0];

}

n = 0 should be n == 0

Page 28: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(31) True or false: the following code is (31) True or false: the following code is legal.legal.

#include <iostream.h>#include <iostream>main(){

cout << “Hello World”;return 0;

}

True, it is legal. The cout called is the one in iostream.h

Page 29: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(32) What is wrong with the following (32) What is wrong with the following code? code?

string &operator.(const string &s, const string &t){

string temp(s);temp.append(t);

return t;}

It is wrong because a reference to a local variable is returned. But that local object passes out of scope when the function is completed.

Page 30: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(33) For each of the following state (33) For each of the following state whether the code is legal or illegal and say whether the code is legal or illegal and say why.why.

(a) int f1(int x);

main(){f1(5) = 6;

}

Illegal, since f1 returns a temp value (hence no lvalue).

Page 31: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(b)int &f2(int x);main(){

f2(5) = 6;}

Legal, since f2 returns an lvalue.

(c) const_int &f3(int x);main(){

f3(5) = 6;}

Illegal, since f3 returns a const (no lvalue).

Page 32: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(34) What’s wrong with the following (34) What’s wrong with the following code?code?

template <class Container>void fun(const Container& x){

Container::iterator i;for(i = x.begin(); i != x.end(); i++)

cout << *i;}

The compiler won’t know what kind of object Container::iterator is. Instead, one must use:

typename Container::iterator i;

Page 33: C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1

(35) You have a string s. Write one line of (35) You have a string s. Write one line of C++ code that prints (to standard output) C++ code that prints (to standard output) the string in reverse order.the string in reverse order.

reverse_copy(s.begin(), s.end(), ostream_iterator<char>(cout));