C++ Object Oriented 1.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism CS351 – Programming Paradigms.
OBJECT ORIENTED PROGRAMMING
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Classes C++ representation of an object
Objects as a programming concept
Inheritance and Polymorphism
Object-Oriented Programming
Review: Two Programming Paradigms
C# - Inheritance and Polymorphism
Inheritance in Java.
Chapter 5 Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Learning Objectives Inheritance Virtual Function.
Chapter 6 Methods: A Deeper Look
Java Programming Language
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Advanced Java Topics Chapter 9
Packages and Interfaces
Overview of C++ Overloading
More Object-Oriented Programming
Dr. Bhargavi Dept of CS CHRIST
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Inheritance.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Classes C++ representation of an object
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

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 the central feature of C++ that supports object-oriented programming. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class. 2

Class and Object (cont) The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class. 3

Class and Object (cont) C++ Class Definitions: When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object. For example, we defined the Box data type using the keyword class as follows: 4

Class and Object (cont) Define C++ Objects: A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box: Both of the objects Box1 and Box2 will have their own copy of data members. 5

Class and Object (cont) Accessing the Data Members: The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear: 6

Class and Object (cont) It is important to note that private and protected members can not be accessed directly using direct member access operator (.). 7

Inheritance One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, we can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. 8

Inheritance (cont) The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. Base & Derived Classes: A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class. class derived-class: access-specifier base-class 9

Inheritance (cont) Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. Please consider a base class Shape and its derived class Rectangle as follows: 10

Inheritance (cont) 11

Inheritance (cont) Access Control and Inheritance: A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. 12

Inheritance (cont) Type of Inheritance: When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied: 13

Inheritance (cont) Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. 14

Inheritance (cont) Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class. 15

Inheritance (cont) Multiple Inheritances: In C++ class can inherit members from more than one class and here is the extended syntax: class derived-class: access baseA, access baseB….. 16

Inheritance (cont) 17

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 Different definitions must be distinguished by their signatures (otherwise which to call is ambiguous) Reminder: signature is the operator/function name and the ordered list of its argument types E.g., add(int,long) and add(long,int) have different signatures E.g., add(const Base &) and add(const Derived &) have different signatures, even if Derived is-a Base Most specific match is used to select which one to call 18

Overloading VS. Overriding Overriding a base class member function is similar to overloading a function or operator But for overriding, definitions are distinguished by their scopes rather than by their signatures C++ can distinguish method definitions according to either static or dynamic type Depends on whether a method is virtual or not Depends on whether called via a reference or pointer vs. directly on an object Depends on whether the call states the scope explicitly (e.g., Foo::baz();) 19

Function Overloading You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type. Following is the example where same function print() is being used to print different data types: 20

Function Overloading (cont) 21

Polymorphism C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. Consider the following example where a base class has been derived by other two classes and area() method has been implemented by the two sub-classes with different implementation. 22

Polymorphism (cont) 23

Polymorphism (cont) When the above code is compiled and executed, it produces the following result: The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static linkage - the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program. 24

Polymorphism (cont) But now, let's make a slight modification in our program and precede the declaration of area() in the Shape class with the keyword virtual so that it looks like this: 25

Abstraction Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details. For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data. You can implement a class with public and private members is an example of data abstraction. 26

Abstraction (cont) 27

Encapsulation Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object. Data Encapsulation Example: Any C++ program where you implement a class with public and private members is an example of data encapsulation and data abstraction. Consider the following example: 28

Encapsulation (cont) 29