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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
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.
Chapter 10 Classes Continued
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.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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
Chapter 10 Inheritance and Polymorphism
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Introduction to Object-Oriented Programming Lesson 2.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
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
Overview of C++ Polymorphism
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.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
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,
OOP: Encapsulation &Abstraction
Object-Oriented Programming
C# - Inheritance and Polymorphism
Chapter 5 Classes.
Object Oriented Programming
Learning Objectives Inheritance Virtual Function.
Packages and Interfaces
Overview of C++ Overloading
Polymorphism Polymorphism
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
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

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 Class and Object (cont)

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

 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 Inheritance (cont)

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 Overview of C++ Overloading

19 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();)

20 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:

21 Function Overloading (cont)

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

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 Polymorphism (cont)

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

27 Abstraction (cont)

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

29 Encapsulation (cont)