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

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Introduction to Programming Lecture 39. Copy Constructor.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
C++ Training Datascope Lawrence D’Antonio Lecture 6 An Overview of C++: What is Polymorphism? - Coercion.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ Yeting Ge.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Overview of C++ Polymorphism
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ INTERVIEW QUESTIONS
7. Inheritance and Polymorphism
Type Systems Terms to learn about types: Related concepts: Type
Exceptions 2 CMSC 202.
Hank Childs, University of Oregon
Abstraction: Generic Programming, pt. 2
Constructors and Destructors
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Overview of C++ Polymorphism
CS410 – Software Engineering Lecture #5: C++ Basics III
Type Systems Terms to learn about types: Related concepts: Type
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

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

 (1) What Danish computer scientist invented C++?  Bjarne Stroustrup  (2) What was the first object-oriented programming language?  Simula

 (3) What does STL stand for and who invented it?  Standard Template Library, Alex Stepanov  (4) (a) Which language is older: C++ or Java?  C++ (sort of)  (b) Which language is older: C++ or Smalltalk?  Smalltalk

 (5) True or false: C++ was designed to be backwards compatible with C.  True (a least in theory, in practice is another story)  (6) What is the difference between overloading and polymorphism?  Overloading creates many functions of the same name that work on different types. Polymorphism creates one function that work on different types.

 (7) True or false: Only object-oriented programming languages can exhibit polymorphism.  False, many languages exhibit polymorphism. In Lisp, car and cons are examples of polymorphic functions.

 (8) What is the difference between function overloading and function overriding?  Overloading involves functions of the same name having different signatures. Overriding occurs in when a function in a derived class replaces a function of the same name in the base class.

 (9) What is a function object?  A function object is an object that may be called like a function. In C++ this is achieved by overloading the () operator.  (10) How does a function object differ from a function?  A function object has state. It can be initialized through a constructor and retains its state between invocations.

 (11) Which of the following C++ mechanisms is used to create polymorphism?  Virtual functions X Templates X  Virtual functions exemplify inclusion polymorphism. A reference or pointer to a parent object may be converted to an object of any inherited type. This means that determining which method is being called is a run-time decision.  Templates implement the concept of parametric polymorphism. Code works correctly for a variety of datatypes. Functional languages such as ML, Haskell, Lisp typically use parametric polymorphism.

 (12) What is a predicate in C++?  A function or function object that returns a boolean.  (13) What is the primary difference between a struct and a class in C++?  In a struct, access is public by default whereas in a class it is private by default.

 (14) What is the meaning of the key word mutable?  A data member of a class is declared to be mutable then it may be modified inside a const member function.

Mutable example Ex: class Image { private: mutable bool is_loaded; public: void Redraw() const { if (is_loaded == false) { //…load image from disk is_loaded = true } //..paint image on screen } };

 (15) What are the member functions that the compiler will write for you?  Default constructor, copy constructor, assignment operator, destructor  (16) True or false: a default constructor cannot take any arguments  False. A default constructor must either have no arguments or all arguments must have default values

 (17) What are the four cast operators in C++?  static_cast, dynamic_cast, reinterpret_cast, const_cast

 (18) Which of the following conversions is generally not legal?  (a) Converting a base object to a derived object?  Not legal (the base object is not a derived object).  (b) Converting a derived object to a base object?  Legal (the derived object is a base object).

 (19) What is an exception specification?  A specification of what types of exceptions a function may throw (or none at all). void f() throw(int); //throws only an int void g() throw(); //throws no exceptions

 (20) What is a nothrow function and how do you declare it?  A nothrow function “guarantees” that it won’t throw an exception. The only example currently allowed is the nothrow new operator.

nothrow 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) { //… }

 (21) True or false: it is illegal for a destructor to throw an exception.  False, but it is generally a bad idea.

 (22) How do you declare and allocate memory for a two dimensional array A with n rows and m columns (where n,m are integer variables determined at run-time)? int **A; A = new *int[n]; for(int i = 0; i < n; i++) A[i] = new int[m];

 (23) What is the most significant difference between malloc() (the C dynamic memory allocation function) and new() (the C++ dynamic memory allocation operator)?  malloc() doesn’t call a constructor for allocated objects, while new() does.

 (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.

 (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.

 (25) What is a smart pointer?  A smart pointer is an object that acts like a pointer. A smart pointer can have state, so it can be responsible for owning what it points to. auto_ptr is an example of a smart pointer in the standard library.

 (26) What is wrong with this code? int *p = new int[10]; //do stuff with p delete p; In order to delete an array, one must use the syntax delete [] p;

 (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.

 (28) (a) What is wrong with the following 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;

 (30) What is wrong with the following function? int foo(const vector &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

 (31) True or false: the following code is legal. #include main() { cout << “Hello World”; return 0; } True, it is legal. The cout called is the one in iostream.h

 (32) What is wrong with the following 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.

 (33) For each of the following state whether the code is legal or illegal and say why. (a) int f1(int x); main() { f1(5) = 6; } Illegal, since f1 returns a temp value (hence no lvalue).

(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).

 (34) What’s wrong with the following code? template 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;

 (35) You have a string s. Write one line of C++ code that prints (to standard output) the string in reverse order. reverse_copy(s.begin(), s.end(), ostream_iterator (cout));