Chapter 5. Object- Oriented Programming Part I

Size: px
Start display at page:

Download "Chapter 5. Object- Oriented Programming Part I"

Transcription

1 Chapter 5 Object- Oriented Programming Part I

2 5: Preview basic terminology comparison of the Java and C++ approaches to polymorphic programming techniques introduced before in the context of inheritance: destructing objects, overloading assignment operators, overloading operations, and exception handling abstract classes, and Template Method design pattern a friend operation and class

3 5: Basic Terminology and a Derived Class Definition Class D extends another class B: D is called a derived class of B B is called a base class of D (B is also called a superclass for the subclass D). BankAccount class has two derived classes: CheckingAccount and SavingsAccount. B D

4 5: Basic Terminology and a Derived Class Definition Java C++ Comments about C++ class D extends B { class D extends B { class D extends B { class D: public B { class D: private B { class D:protected B { A public inheritance A private inheritance A protected inheritance super -- C++ does not support super final -- C++ does not support final

5 5: Public Inheritance Public inheritance: a derived class wants to inherit the entire public interface of its base class (same as Java inheritance). All features that are public in the base class can be used by the derived class.

6 class Student { public: Public Derivation Student(long = 0, string = ""); long getnumber() const; string getname() const; void printinfo() const; private: long number_; string name_; }; Public derivation class StudentWithAccount : public Student { // no extends public: StudentWithAccount(long, string, double = 0.0); double getbalance() const; void setbalance(double); void printinfo() const; private: double balance_; Last slide };

7 5: Creating Objects The constructor of a derived class must first take care of the initialization of the attributes of its base class, using syntax similar to the member initialization list: StudentWithAccount::StudentWithAccount(long number, string name, double balance) : Student(number, name) // base class' constructor { balance_ = balance; } StudentWithAccount::StudentWithAccount(long number, string name, double balance) : Student(number, name), balance_(balance) { } // alternative implementation

8 Derived Class Constructor The constructor of a derived class D calls the constructor of the base class B using syntax similar to the member initialization list: : B(parameters)

9 5: RTTI and Type Conversions Java: a powerful reflection mechanism C++: a modest set of operations, Run-Time Type Identification, RTTI to allow you to find the current type of the value of a pointer or a reference, whose declared type is of some base class. There are two basic RTTI mechanisms: - a dynamic cast, - typeid RTTI works only if used for classes that have at least one virtual function. In general, RTTI should be avoided and often can be replaced by using polymorphic operations.

10 5: Downcasting Pointers and references to a base class can be assigned objects of a derived class without using any explicit cast: Student* ps = new StudentWithAccount(40, "john", 2000); Student& s = StudentWithAccount(30, "barbara", 1000); cout << s.getname(); cout << s.getbalance(); cout << ps->printinfo(); // can't access derived class // calls Student::printInfo() Student's code

11 5: dynamic_cast To find the type of the variable value at run-time: dynamic_cast<t>(p) If p points to some object of type T, then the above expression returns p converted to T, and 0 otherwise. This downcast can be applied only to pointers: Student* ps;... // initialize ps StudentWithAccount* pswa; if(pswa = dynamic_cast<studentwithaccount*>(ps)) { cout << pswa->getbalance(); pswa->printinfo(); // calls StudentWithAccount::printInfo(); } else // pswa is 0

12 5: dynamic_cast There are several reasons for which dynamic cast should be avoided: it is a run-time operation, more expensive than a compile-time operation, and it may be difficult to come up with good error handling if this cast fails downcasting using dynamic cast makes program extensibility and maintenance more difficult, because you have to make assumptions about the inheritance hierarchy, and if this hierarchy changes, your code will likely break.

13 5: Upcast Upcast: moving up the inheritance tree a cast from a derived class to a base class (allowed only for a public inheritance): void print(student s) { s.printinfo(); } StudentWithAccount swa(30, "barbara", 1000); print(swa); // swa is upcast'ed; s = (Student)swa Results in a slice of the object swa being copied to the object s Calls Student::printInfo() rather than StudentWithAccount::printInfo()

14 5: Slicing Student(long = 0, string = ""); long getnumber() const; string getname() const; void printinfo() const; long number_; string name_; StudentWithAccount(long, string, double = 0.0); double getbalance() const; void setbalance(double); void printinfo() const; double balance_; swa Student(long = 0, string = ""); long getnumber() const; string getname() const; void printinfo() const; long number_; string name_; swa sliced to Student

15 5: Copy Constructors The syntax used to define a constructor in a derived class is also used to define a copy constructor; for example: Student::Student(const Student&) {... } StudentWithAccount(const StudentWithAccount& swa) : Student(swa) { balance_ = swa.balance_; Call base class' copy } constructor

16 5: Scope and Visibility Modifiers Using public inheritance: a derived class inherits all of the public features of the base class private features of the base class are not accessible in the derived class void StudentWithAccount::printInfo() const { cout << "Student number:" << number_ << "; name:" << name_ << "; balance:" << balance_ << endl; } any future changes to the private part of the base class do not affect a derived class known as inheriting the interface, but not the implementation

17 5: Scope and Visibility Modifiers (cont.) You can use the scope operator :: to refer to a public feature in a base class that has the same name as a feature in a derived class: void StudentWithAccount::printInfo() const { Student::printInfo(); // access feature from base class cout << "balance:" << balance_ << endl; }

18 5: Protected Interface As in Java, a C++ class has a public interface, and a protected interface. A class feature may be qualified as protected: private for the instantiating clients accessible to its derived classes. A protected feature f may be accessed in member operations of a derived class only in the following two contexts: - direct access of the form: f or this->f - indirect access of the form: ref.f or ref->f, where ref is (or, points to) an instance of a derived class.

19 5: Protected Interface (cont.) If both number_ and name_ in the class Student are protected: void StudentWithAccount::printInfo() const { cout << "Student number:" << number_ << "; name:" << name_ << "; balance:" << balance_ << endl; } void StudentWithAccount::info(const Student& s, const StudentWithAccount& swa) { cout << s.number_; // can't access through Student cout << swa.number_; // can through StudentWithAccount }

20 In Java, all operations that are not final employ late binding. Given a variable var and the call var.foo() 5: Polymorphism and Virtual Functions it is the value of var, and not its type that is used to find foo(). If the value of the variable is an object of a derived class that redefines an operation defined in a base class, then this redefined operation is invoked. For final operations, Java uses early binding: the type of the class variable, and not its value, determines which operation is invoked. C++ uses a different philosophy: by default, early binding is used. Late binding is used only for pointers or references, and only if the operation is explicitly specified as virtual

21 5: Polymorphism and Virtual Functions (cont.) Student s(10, "kasia"); Student* ps = new Student(20, "michael"); StudentWithAccount sw(30, "barbara", 1000); StudentWithAccount* psw = new StudentWithAccount(30, "mary", 0); Student* pstud = new StudentWithAccount(40, "john", 2000); s.printinfo(); // Student::printInfo ps->printinfo(); // Student::printInfo sw.printinfo(); // StudentWithAccount::printInfo psw->printinfo(); // StudentWithAccount::printInfo pstud->printinfo(); // Student::printInfo Student's code

22 5: Polymorphism and Virtual Functions (cont.) In order to use late binding, printinfo() must be made virtual: class Student { public:... virtual void printinfo() const; }; void Student::printInfo() const { // don't repeat virtual cout << "Student number:" << number_ << "; name:" << name_ << endl; } class StudentWithAccount : public Student { public: virtual void printinfo() const; // virtual

23 void StudentWithAccount::printInfo() const { } 5: Polymorphism and Virtual Functions (cont.) cout << "Student number:" << number_ << "; name:" << name_ << "; balance:" << balance_ << endl; void StudentWithAccount::printInfo() const { Student::printInfo(); cout << "balance:" << balance_ << endl; } because the scope operator turns off late binding.

24 Virtual 1. When a virtual operation is invoked from the base class' constructor, then it is not really virtual (i.e. early binding is used). 2. A virtual operation in a derived class must have an operation signature identical to the corresponding operation signature in a base class (with the exception of Covariant Return Types)

25 5: Destructing Objects Destructors: are not inherited you cannot explicitly call a destructor of a base class in the definition of a destructor of the derived class it is easy to end up writing a destructor that will only be called for the base class; a grave mistake if the derived class has any resources that need to be released.

26 Virtual Destructor When you design a class, make the destructor virtual; otherwise, it may be difficult or even impossible to design derived classes that clean up all memory. If this base class has no resources, then make the destructor's body empty.

27 class AccountForStudent { public: Virtual destructor virtual ~AccountForStudent();... protected: Student* stud_; }; AccountForStudent::~AccountForStudent() { delete stud_;} class Bank; class BankAccount : public AccountForStudent { public: virtual ~BankAccount();... protected: Bank* bank_; }; BankAccount::~BankAccount() { delete bank_; }

28 5: Destructing Objects AccountForStudent* ba = new BankAccount("John", 100, , new Bank("Royal Bank")); maintains two resources: a bank and a student. For delete ba; the value of ba is used (late binding), and so the destructor of the derived class gets called. When the destructor of a derived class completes, the destructor of the base class gets called.

29 5: Overloaded Assignments

30 class AccountForStudent { public: Overloaded Assignment AccountForStudent& operator=(const AccountForStudent&); protected: Student* stud_; }; AccountForStudent& AccountForStudent::operator=( const AccountForStudent& as) { if(this == &as) return *this; delete stud_; stud_ = new Student(as.stud_->getNumber(), as.stud_->getname()); balance_ = as.balance_; return *this; }

31 class BankAccount : public AccountForStudent { public: Overloaded assignment BankAccount& operator=(const BankAccount&); protected: Bank* bank_; }; BankAccount::operator=(const BankAccount& b) { if(this == &b) return *this; AccountForStudent::operator=(b); // assign base part // copy bank delete bank_; bank_ = new Bank(b.bank_->getName()); return *this; }

32 Overloaded Assignment in Derived Class An overloaded assignment operator in a derived class must make an explicit call to the overloaded assignment operator in the base class.

33 5: Overloading Overloading an operation in a derived class means that the operation from the base class becomes inaccessible: class IntegerReadOnly { public: explicit IntegerReadOnly(int); int value() const; // query protected: int value_; }; class Integer : public IntegerReadOnly { public: explicit Integer(int); int value(int); // modifier, returns new value };

34 5: Overloading Integer i(10); cout << i.value(5); // modifier: sets the value to 5 cout << i.value(); // wrong; accessor is hidden To provide access to the hidden operation from the base class: Or: int Integer::value() { return IntegerReadOnly::value(); } using BaseClass::featureName; // no brackets

35 5: Overloading class Integer : public IntegerReadOnly { public: Integer(int); int value(int); // modifier using IntegerReadOnly::value; // int value() accessible }; cout << i.value(5); // modifier: sets the value to 5 cout << i.value(); // accessor

36 5: Overriding Overriding virtual functions is used to achieve polymorphism. The signature of a virtual operation in a derived class must be identical to the signature of this operation defined in the base class: class IntegerMutable { public: IntegerMutable(int); virtual int value(int = 0); //modifier, returns value protected: int value_; };

37 5: Overriding class Integer : public IntegerMutable { public: Integer(int); virtual int value() const; // different signature }; IntegerMutable* i = new Integer(10); cout << i->value(); // modifier: sets the value to 0 The query operation value() is invoked from the base class because it is overloaded instead of being overridden.

38 5: Covariant Return Types If a virtual operation in the base class returns a reference or a pointer to a class type, then signatures in the base class and in the derived class don't have to be strictly identical: class Base { class Derived : public Base { public: public: virtual Base* foo(); virtual Base* foo(); virtual Base* goo(); virtual Derived* goo();... void doo(); }; };

39 5: Covariant Return Types Base* p = new Derived; p->goo()->doo(); p->foo()->doo(); // foo()'s return type is Base if(derived* d = dynamic_cast<derived*>(p->foo())) d->doo();

40 Overloading and Overriding 1. Overriding: --A non-virtual operation in a derived class hides an operation with the same signature in an ancestor class. --The hidden operation can be made available through the using keyword. --The implementation of a virtual operation can be replaced in a derived class, provided that the signature is not changed 2. Overloading works only within the scope of a single class

41 5: Passing Parameters by Value and by Reference When a parameter of an operation is passed by value, and the type of the formal parameter is a base class of the type of the actual parameter, then the resulting slicing that occurs may create a problem: class Shape { public: virtual void display() const;... }; class SpecificShape : public Shape { public: virtual void display() const;... };

42 5: Passing Parameters by Value and by Reference void display(shape s) {... s.display(); } SpecificShape ss; display(ss); // the parameter s is sliced The call s.display() invokes Shape::display() rather than SpecificShape::display(). Passing parameters by reference avoids these problems: void display(const Shape& s);

43 5: Standard Exceptions The C++ standard library defines a hierarchy of exceptions, with exception as the root, used by operations in this library. Can also be used in any user defined program (include <stdexcept>). Exceptions are divided into: logic errors: static errors that can be prevented and detected at compile time run-time errors: dynamic errors that can be detected only at run-time. Derived classes of logic errors use self-explanatory names: invalid_argument exception should be used if an operation receives a parameter with an invalid value, such as 0.

44 5: Standard Exceptions

45 5: Standard Exceptions exception has an operation: virtual const char* what() const throw(); Each of the exception classes has an explicit constructor, used to specify the value returned by what(): logic_error::logic_error(const string& argumentforwhat); There is a similar arrangement for run-time errors.

46 class IntStack { public: IntStack(int = 100); IntStack // default size IntStack(const IntStack&); virtual ~IntStack(); IntStack& operator=(const IntStack&); void push(int) throw(logic_error); int pop() throw(logic_error); private: int top_; int* stack_; int size_; }; void IntStack::push(int i) throw(logic_error) { if(top_ == size_ - 1) // full throw logic_error("stack full "); stack_[++top_] = i; }

47 5: Resource Management, Part II Throwing an exception results in an unwinding of the run-time stack, which means that all objects created between the time the exception is thrown and caught are destroyed through a call to their destructors. thrown objects time caught: Destruct!

48 5: Resource Management, Part II (cont.) try { IntStack* s = new IntStack(20); s->push(20); } catch(const bad_alloc&) { cout << "ran out of memory" << endl; } catch(const logic_error& e) { cout << e.what() << endl; }

49 5: Resource Management, Part II (cont.) You can create a wrapper class Resource for a pointer to a resource: typedef Resource* ResourcePointer; class ResourceHandle { public: ResourceHandle(const ResourcePointer&); virtual ~ResourceHandle(); operator ResourcePointer() const; private: ResourcePointer handle_; ResourceHandle(const ResourceHandle&); ResourceHandle& operator=(const ResourceHandle&); };

50 ResourceHandle::ResourceHandle(const ResourcePointer &r) : handle_(r) {} ResourceHandle::~ResourceHandle() { delete handle_; 5: Resource Management, Part II (cont) } ResourceHandle::operator ResourcePointer() const { return handle_; } - makes implicit type conversions possible: ResourceHandle r(new ResourcePointer()); void op(resourcepointer); op(r); // gets converted to op(r.handle_)

51 5: Resource Management, Part II (cont) void foo() { Resourcehandle r(new Resource); op(r);... } If an exception is thrown in foo(), then the function will terminate and all local destructors will be called. In particular, the destructor for r will be called, releasing the handle_ resource.

52 5: Abstract Operations and Classes Java C++ Comments about C++ class A { abstract void f(); class A { virtual void f()=0; Pure virtual operation, with empty body is abstract abstract class A class A { // abstract virtual void f()=0; interface Ifc class Ifc { //interface virtual void f()=0; class Imp implements Ifc class Imp : public Ifc { // implements A class with at least one pure virtual operation A class with all operations that are pure virtual A derived class that implements all abstract operations

53 5: Benchmark class BenchmarkClass { // abstract public: virtual void benchmark() const = 0; double repeat(long count) const; }; #include <ctime> #include "ex4.4.benchmarkclass.h" double BenchmarkClass::repeat(long count) const { time_t start, finish; time(&start); for(long loop = 0; loop < count; ++loop) benchmark(); time(&finish); return difftime(finish, start); }

54 class MyBenchmarkClass : public BenchmarkClass { public: Benchmark virtual void benchmark() const; }; void MyBenchmarkClass::benchmark() const { double res = 1.23 * 4.56; } BenchmarkClass* b = new MyBenchmarkClass(); cout << b->repeat( ); // implements Here - an abstract class has a complete interface, and only provides the implementation of those operations that can be specified at this general level. These implementations may use other operations defined in this class, both concrete and abstract - the derived classes are responsible for implementing the abstract operations.

55 5: Template Method Design Pattern If you have two similar operations in a class, you can define a single common operation in the base class. Operations that are not similar are expressed as abstract, to be defined in derived classes. The template method design pattern: the template method defines the algorithm in terms of abstract operations, called the primitive operations. The Template Method Design Pattern is an example of a class behavioral pattern, because it describes class behavior. In general, behavioral design patterns describe patterns of objects, classes, and the communication between them. There are two kinds of behavioral patterns: class patterns that use inheritance, and object patterns that use object composition

56 5: Template Method Design Pattern (cont.) The Template Method Design Pattern: - one or more abstract (primitive) operations that represent the missing information; - remaining template methods use these abstract operations. The derived classes are responsible for implementing these abstract operations

57 5: Template Method Design Pattern (cont.) Examples of template method design pattern: the implementation of sorting algorithms, defined in terms of a primitive compare() operation to compare two elements. The abstract base class has a template method sort(), which uses compare(). Various derived classes of this class then define different ways of comparing elements. a class responsible for logging users into an application; the login operation is a template method and consists of four steps: - prompting for the user id and password (concrete) - authenticating the user (primitive) - displaying appropriate information while the authentication is in progress (concrete) - notification that the login is complete (primitive).

58 5: Template Method Design Pattern (cont.) the template method should not be overridden, and therefore it is not defined as virtual all operations that are used by the template method are defined as virtual, as well as private or protected, so that the client cannot directly call them. a derived class may also redefine (override) other concrete operations: concrete operations overridden in derived classes are called hook operations. In Java s java.io.reader class, read() is a hook operation, overridden in java.io.inputstreamreader.

59 5: Postponing Creation of Objects 1. By specifying at least one operation as abstract (i.e. pure virtual) 2. By specifying constructors as private or protected 3. Defining a pure virtual destructor: class B { public: virtual ~B() = 0;... }; B::~B() { }

60 Consider two classes, which cooperate to perform a certain task: List and ListElement. 5: Friends The class List needs access to the private attributes of the class ListElement, and it would be inefficient to use queries and modifiers for this purpose. C++ provides a friend declaration, using which the class may declare that one of the following constructs is a friend and therefore has access to private features of this class: - another class - a single operation from another class - a global operation.

61 ListElem gives access to its private features to the entire List: class ListElem { friend class List;... }; // gives access to List To give access to a specific operation in this class, say show(): class ListElem { friend void List::show();... }; To give access to a global function print() class ListElem { friend void ::print();... }; 5: Friends (cont.) // gives access to show() // gives access to print()

62 5: Friends (cont.) external operations specified as friends are not a part of a class interface, but they have the same access rights as regular class operations friendship is limited to the class specified as a friend. When you declare that X is a friend, you do not give special privileges to X's friends friendship is not transitive (nor is it symmetric: If Y is a friend of X then X is not automatically a friend of Y) a derived class of X is not considered to be a friend friendship cannot be declared for an existing class, because only this class can specify it. Note that friends do not completely break encapsulation; but you should avoid declaring friendship unless absolutely necessary

Chapter 3. Object-Based Programming Part I

Chapter 3. Object-Based Programming Part I Chapter 3 Object-Based Programming Part I 3: Preview Object-based programming unlike object-oriented programming does not use extended (derived) classes. This chapter covers: similarities and differences

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers Resource Management Resource Management With a Unique Pointer Dynamic memory is an example of a resource that is allocated and must be released. void f() { Point* p = new Point(10, 20); // use p delete

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing

More information

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

C++ Programming: Polymorphism

C++ Programming: Polymorphism C++ Programming: Polymorphism 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Run-time binding in C++ Abstract base classes Run-time type identification 2 Function

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

Inheritance, Polymorphism and the Object Memory Model

Inheritance, Polymorphism and the Object Memory Model Inheritance, Polymorphism and the Object Memory Model 1 how objects are stored in memory at runtime? compiler - operations such as access to a member of an object are compiled runtime - implementation

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded

More information

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING YEAR/SEM:II & III UNIT I 1) Give the evolution diagram of OOPS concept. 2) Give some

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University Polymorphism Miri Ben-Nissan (Kopel) 1 Shape Triangle Rectangle Circle int main( ) Shape* p = GetShape( ); p->draw( ); Shape* GetShape( ) choose randomly which shape to send back For example: Shape* p

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p. Preface to the Second Edition p. iii Preface to the First Edition p. vi Brief Contents p. ix Introduction to C++ p. 1 A Review of Structures p. 1 The Need for Structures p. 1 Creating a New Data Type Using

More information

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) { TDDD38 APiC++ Exception Handling 134 Exception handling provides a way to transfer control and information from a point in the execution to an exception handler a handler can be invoked by a throw expression

More information

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding Conformance Conformance and Class Invariants Same or Better Principle Access Conformance Contract Conformance Signature Conformance Co-, Contra- and No-Variance Overloading and Overriding Inheritance as

More information

Chapter 6. Object- Oriented Programming Part II

Chapter 6. Object- Oriented Programming Part II Chapter 6 Object- Oriented Programming Part II 6: Preview issues surrounding the object creational process the Abstract Factory design pattern private and protected inheritance multiple inheritance comparison

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 Virtual Functions in C++ Employee /\ / \ ---- Manager 2 Case 1: class Employee { string firstname, lastname; //... Employee( string fnam, string lnam

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CGS 2405 Advanced Programming with C++ Course Justification

CGS 2405 Advanced Programming with C++ Course Justification Course Justification This course is the second C++ computer programming course in the Computer Science Associate in Arts degree program. This course is required for an Associate in Arts Computer Science

More information

Object Oriented Programming with c++ Question Bank

Object Oriented Programming with c++ Question Bank Object Oriented Programming with c++ Question Bank UNIT-1: Introduction to C++ 1. Describe the following characteristics of OOP. i Encapsulation ii Polymorphism, iii Inheritance 2. Discuss function prototyping,

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

VALLIAMMAI ENGINEERING COLLEGE

VALLIAMMAI ENGINEERING COLLEGE VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 5 Part One: Exception Handling 2016 www.cse.unsw.edu.au/ cs6771 2.... Memory Management & Exception Handling.1 Part I: Exception Handling Exception objects

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Design issues for objectoriented. languages. Objects-only pure language vs mixed. Are subclasses subtypes of the superclass? Encapsulation Encapsulation grouping of subprograms and the data they manipulate Information hiding abstract data types type definition is hidden from the user variables of the type can be declared variables

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming 1. What is object-oriented programming (OOP)? OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object

More information

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 5 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Templates S Function and class templates you specify with a single code segment an entire

More information

Handout 9 OO Inheritance.

Handout 9 OO Inheritance. Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 1 of 11 Handout 9 OO Inheritance. All classes in Java form a hierarchy. The top of the hierarchy is class Object Example: classicalarchives.com

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 19 November 7, 2018 CPSC 427, Lecture 19, November 7, 2018 1/18 Exceptions Thowing an Exception Catching an Exception CPSC 427, Lecture

More information

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int, Classes Starting Savitch Chapter 10 l l A class is a data type whose variables are objects Some pre-defined classes in C++ include int, char, ifstream Of course, you can define your own classes too A class

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 Introduction 2 IS 0020 Program Design and Software Tools Exception Handling Lecture 12 November 23, 200 Exceptions Indicates problem occurred in program Not common An "exception" to a program that usually

More information

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading HOW C++ WORKS Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++

More information

Inclusion Polymorphism

Inclusion Polymorphism 06D-1 Inclusion Polymorphism Polymorphic code Polymorphic references Up- and Down- Casting Polymorphism and values Polymorphic Arrays Inclusion Polymorphism in Context 06D-2 Polymorphism Ad hoc Universal

More information

04-24/26 Discussion Notes

04-24/26 Discussion Notes 04-24/26 Discussion Notes PIC 10B Spring 2018 1 When const references should be used and should not be used 1.1 Parameters to constructors We ve already seen code like the following 1 int add10 ( int x

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std; - 147 - Advanced Concepts: 21. Exceptions Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers.

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

class Polynomial { public: Polynomial(const string& N = no name, const vector<int>& C = vector<int>());... }; Default Arguments 1 When declaring a C++ function, you may optionally specify a default value for function parameters by listing initializations for them in the declaration: class Polynomial { public:

More information

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies

Distributed Real-Time Control Systems. Chapter 13 C++ Class Hierarchies Distributed Real-Time Control Systems Chapter 13 C++ Class Hierarchies 1 Class Hierarchies The human brain is very efficient in finding common properties to different entities and classify them according

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

6 Architecture of C++ programs

6 Architecture of C++ programs 6 Architecture of C++ programs 1 Preview managing memory and other resources "resource acquisition is initialization" (RAII) using std::auto_ptr and other smart pointers safe construction of an object

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Problem Solving with C++

Problem Solving with C++ GLOBAL EDITION Problem Solving with C++ NINTH EDITION Walter Savitch Kendrick Mock Ninth Edition PROBLEM SOLVING with C++ Problem Solving with C++, Global Edition Cover Title Copyright Contents Chapter

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading How C++ Works 1 Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object Polymorphism C++ Object Oriented Programming Pei-yih Ting NTOU CS 26-1 Contents Assignment to base / derived types of objects Assignment to base / derived types of pointers Heterogeneous container and

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information