Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your 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.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 11 Inheritance and Polymorphism §11.1 Concept of Inheritance §11.2 Accessibility in Inheritance §11.3 Constructor/Destructor in Inheritance §11.4.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
A First Book of C++ Chapter 12 Extending Your Classes.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Design issues for Object-Oriented Languages
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Pointers and Dynamic Arrays
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
Polymorphism &Virtual Functions
Chapter 5 Classes.
Polymorphism & Virtual Functions
Chapter 2 Objects and Classes
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Programming Language
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Java Programming Language
Presentation transcript:

Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors and member functions. 3. What is the purpose of using accessibility control? 4. What are the benefits of data field encapsulation?

Chapter 9 Answers Q1. : A function to create and initialize an object. – In syntax: No return type; Class name; Called automatically by the system Q2.: Omitted. Q3.: to control the access to class members, it is a method to encapsulate data. – To protect the members from access of outside functions. Q4.: – to realize an “object”: variables and functions are bound – to protect data from directly change from outside of a class – to hide detailed information of object properties – to ease the maintenance of code

Chapter 10 Questions (Submit your answers in the class of next week) 1. Is the following class (in (a)) immutable? Why? 2. Modify the following program (in (b)) to output the following text, but you can’t modify the main function. 3 class A{ public: A(){ name = "Xyz"; } string *getName(){ return &name; } private: string name; }; (a) (b) #include using namespace std; int main(){ A a; cout<<"Hello, everyone!\n"; return 0; } Hi, I am coming... Hello, erveryone! Thanks! Bye!

Chapter 10 Questions 3.Where is the copy constructor called in the following program? Please list the line numbers. 4 1 class FOO{ 2 public: 3 FOO(int i){ 4 pointer = new int; 5 *pointer = i; 6 } 7 FOO(const FOO& other){ 8 pointer = new int; 9 *pointer = *other.pointer; 10 } 11 ~FOO(){ delete pointer;} 12 int get(){return *pointer;} 13 FOO getObj(){ 14 FOO obj(0); 15 *obj.pointer = *pointer; 16 return obj; 17 } 18 private: 19 int *pointer; 20 }; 21 void display(FOO obj){cout<<obj.get()<<endl;} 22 int main(){ 23 FOO obj1(15); 24 FOO obj2 = obj1; 25 display(obj2.getObj()); 26 }

Chapter 10 Questions 4.Is there any compiling error in the following program? 5 #include using namespace std; class FOO{ public: FOO(int i){ x=i; } friend void display(FOO obj); private: int x; }; void FOO::display(FOO obj){ cout<<obj.x<<endl; } int main(){ FOO obj(1); display(obj); }

Chapter 10 Answers Q1: No. The function getName() returns the address of name, which can be used to change the value of name although it is a private field. Q2: Q3: Line 16, 25, 24 (if RVO is enabled, Line 24 only) Q4: “error C2039: “display”: 不是 “FOO” 的成员 ” 6 A a; string *ps = a.getName(); ps->append("abc"); cout data()<<endl; class A{ A(){ cout<<"Hi, I am coming...\n“;} ~A(){cout<<"Thanks! Bye!\n“;}

7 Chapter 11 Questions 1.Point out the errors in the following code. Assume the implementation of the classes is correct and omitted due to the limit of space. class BOX{ public: BOX(int, int, int, int, char*, int); ~BOX(); int show(); int hide(); int move(int, int); int zoom(int); protected: int draw(); int start_x, start_y; int width, height; char *title; int color; }; class DialogBox: BOX{ public: DialogBox(int, int, int, int, char*, int, char*, char*); ~DialogBox(); int select(); private: int draw(); char *ok_button; char *cancel_button; }; //…. The Implementation of the classes is omitted.// int main(){ DialogBox dlg(0, 0, 15, 13, "test", 1, "OK", "Cancel"); dlg.move(19, 20); }

8 Chapter 11 Questions (con’t) 2.Write down the output of the following code. class Base1{ public: Base1( int x ){ cout<<"Const. Base1.\n"; value = x; } int getData(){ return value; } protected: int value; }; class Derived :public Base2, public Base1{ public: Derived( int i, char ch, double db ) : Base1( i ), Base2( ch ), real( db ) { cout<<"Const. Derived.\n"; } double getReal(){ return real; } private: double real; }; int main() { Base1 base1( 10 ); Base2 base2( 'Z' ); Derived derived( 7, 'A', 3.5 ); return 0; } class Base2{ public: Base2( char ch ){ cout<<"Const. Base2.\n"; letter = ch; } char getData() const{ return letter; } protected: char letter; };

9 Chapter 11 Questions (con’t) 3.Describe the difference between “virtual function” and “abstract function”. 4.How is the polymorphism enabled? 5.What is the difference between dynamic casting and static casting?

Chapter 11 Answers Q1: “dlg.move(19, 20)” will cause a compiling error. The default inheritance is private, so “move()” is treated as a private member of DialogBox. Q2: 10 Const. Base1. Const. Base2. Const. Base1. Const. Derived.

Chapter 11 Answers Q3: an abstract function is a pure virtual function, i.e. a virtual function without function body. It must be overridden in the derived class. Q4: two elements, pointer of base class and overriding of virtual function. When a pointer of a base class type points to an object of a derived class, and the virtual function is called via the pointer, the function version defined in the derived class rather than the one in the base class would be executed. Q5: Static casting casts numeric types. It is done in the compiling process. If the cast is invalid, a compiling error will occur. Dynamic casting is used to safely cast a base class pointer into a pointer of a derived class. It is done at runtime. If the cast fails because the real type of the object pointed to is not the type of the desired subclass, the cast returns NULL. 11

Chapter 13 Questions 1.Is the following code a correct implementation of “+=”? 2.Why do you need to overload “=”? 12 Rational Rational::operator+=(Rational &secondRational){ this->add(secondRational); return this; }

Answers Q1. No, because: – It does not assign the new value to the current object, i.e. the first operand of “+=”; – It returns a wrong value (the correct value is “*this”). Q2. “=” may be used to “assign” one object to another. Then, a shallow copy is in fact conducted, which may cause problems of sharing pointing target if the constructor or copy constructor involves resources allocation, e.g. the “new” operation. To avoid such problem, we need to overload “=” with deep copy. 13

Chapter 14 Homework Questions 1. Show the output of the code in Question 14.1 (p445) with the input “94”. 2. Given the following code. If an exception is thrown by statement1, will statement2 be excuted? 14 try{ statement1; }catch(…){ } statement2;

Answers Q1. Q2. Yes, if the exception can be caught by the “catch” block; Otherwise, the program may be terminated before the statement Start of try block … End of try block … Continue…

Chapter 15 Homework Questions 1. Design a function template to compute the absolute value of a number. 2. Compare and discuss the advantages/disadvantages of function template and overloading?

Answers A1: A2: 17 template T abs(T orig){ return orig>0?orig:-orig; } int main(){ cout<<abs(0)<<endl; cout<<abs(-1)<<endl; cout<<abs(0.5)<<endl; cout<<abs('a')<<endl; } OverloadingTemplate AdvantageFlexible: Various parameter lists, return types Various function bodies Implicit type conversion Operator overloading Simple: Single function code Flexible: unspecified data type DisadvantageComplex: Repeated code In flexible: only known data type Inflexible: Uniform parameter list Uniform function body, return type