C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
C/c++ 4 Yeting Ge.
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.
Inheritance, Polymorphism, and Virtual Functions
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.
C++ Yeting Ge.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
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 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter -6 Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Learners Support Publications Constructors and Destructors.
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,
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Run time Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Overloading
Constructors and Destructors
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
VIRTUAL FUNCTIONS RITIKA SHARMA.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

C/C++ 3 Yeting Ge

Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp

Static members in class Static variables Shared by all objects Static functions Have access to static members only Static members can be accessed by the class name

Friend functions Have access to the private members of a class. Must be declared as friend in that class. Why friend functions? efficiency 30.cpp 31.cpp 32.cpp

Friend class A class can be declared as the friend of another class.

Function overloading Define several functions of the same name, differ by parameters. void Show() void Show(char *str) Void show(int x) 33.cpp

Function overloading Must have different parameters int func1(int a, int b); double func1(int a, int b); void func(int value); void func(int &value); Static binding The compilers determine which function is called. (Often used for the multiple constructors)

Operator overloading Define new operations for operators (enable them to work with class objects). + - * / = += -= *= /= > >= == != = % & ^ ! | ~ &= ^= |= && || %= [] () new delete Class date x,y X+y x-y x>y, x&y

Special member functions Ret_type class_name::operator<>(arg_list) 34.cpp The return type can be any type.

Overloading = this pointer 35.cpp = & copy constructor

Operator overloading by friend functions Sometimes it is more convenient to use friend functions to overload a binary operator 36.cpp 37.cpp

Overloading examples () cpp 37-2.cpp 37-3.cpp 37-4.cpp

Overloading summary Same name Different parameters Static binding (compile time) Anywhere

Type conversion between classes An object of derived class is an object of the base class. A pointer of the base class can be assigned to an address of the derived class. 38.cpp

Redefine It is possible to redefine a member of the base class in the derived class Rule of scoping 40.cpp

What is the result? 41.cpp The way out 43.cpp

Virtual function & overriding Define a member function to be virtual Use pointer/reference/member functions to call virtual functions Dynamic binding (Time consuming) The constructor cannot be virtual Must be a member function

Virtual functions examples By pointers 42.cpp By reference 43.cpp By member function of the base class 44.cpp

Overloading & overriding Polymorphism Static and dynamic (Compile time and running time) Parameters Anywhere / between the base and derived class

Pure virtual functions & abstract class Pure virtual functions A function declared without definition virtual ret_type func_name(arg_list)= 0; Abstract class A class contains one or more pure functions Can not be instantiated Can be used to define pointers