Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Similar documents
Programming C++ Lecture 2. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

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

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

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

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Friends and Overloaded Operators

Friends and Overloaded Operators

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

Friend Functions, Inheritance

Inheritance, and Polymorphism.

Object-Oriented Design (OOD) and C++

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS304 Object Oriented Programming Final Term

Polymorphism Part 1 1

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

G52CPP C++ Programming Lecture 13

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Crash Course into. Prof. Dr. Renato Pajarola

Inheritance and Polymorphism

CS250 Final Review Questions

CSC1322 Object-Oriented Programming Concepts

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CS250 Final Review Questions

Chapter 18 - C++ Operator Overloading

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Object Oriented Design

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

Short Notes of CS201

W3101: Programming Languages C++ Ramana Isukapalli

CS201 - Introduction to Programming Glossary By

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Data Abstraction. Hwansoo Han

Classes in C++98 and C++11

C++ Important Questions with Answers

Operator overloading

Programming, numerics and optimization

Where do we go from here?

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Classes: A Deeper Look. Systems Programming

Function Overloading

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Recharge (int, int, int); //constructor declared void disply();

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

Evolution of Programming Languages

8. Object-oriented Programming. June 15, 2010

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

AN OVERVIEW OF C++ 1

CS 247: Software Engineering Principles. ADT Design

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Data Structures (INE2011)

COEN244: Class & function templates

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Programming Language Concepts: Lecture 2

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Computer Programming Inheritance 10 th Lecture

Module Operator Overloading and Type Conversion. Table of Contents

Review: C++ Basic Concepts. Dr. Yingwu Zhu

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

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

CS250 Final Review Questions

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Constructor - example

C++ (classes) Hwansoo Han

2 ADT Programming User-defined abstract data types

Overloading Operators in C++

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

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

Object Oriented Software Design II

CS24 Week 3 Lecture 1

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CS 162, Lecture 25: Exam II Review. 30 May 2018

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Object Oriented Software Design II

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

Midterm Exam 5 April 20, 2015

Ch02. True/False Indicate whether the statement is true or false.

Lecture 18 Tao Wang 1

CS3157: Advanced Programming. Outline

Lecture 7. Log into Linux New documents posted to course webpage

Introduction to Programming Using Java (98-388)

Data Structures using OOP C++ Lecture 3

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

ECE 122. Engineering Problem Solving with Java

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Introduction Of Classes ( OOPS )

Use the dot operator to access a member of a specific object.

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Chapter 19 - C++ Inheritance

Programming in C++: Assignment Week 8


Transcription:

Programming C++ Lecture 3 Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S

Inheritance S Software reuse inherit a class s data and behaviors and enhance with new capabilities. S Existing class = base class, inheriting class = derived class (no super/ subclass like Java) S Derived class is more specialized than base class. Object instances of derived class are also object of base class (All cars are vehicles, but not all vehicles are cars.) S There can be multiple levels of inheritance. 2

Inheritance Details S class Circle : public Shape S What is base, what is derived here? S Default = public inheritance (base member variables retain same access level in derived class), but there are other types S When redefine something in derived class, use <baseclassname>::member to access base class s version. 3

Inheritance and Member Variables S Derived class has all attributes of base class. S Derived class can access non-private members of base class. S protected members of base class are accessible to members and friends of any derived classes. S Derived does not inherit constructor or destructor of base. S Derived class can re-define base-class member functions for its own purposes, customizing base class behaviors. S Size of derived class = non-static data members of derived class + non-static data members of base class (even if private) 4

Base Class Example class Member { public: Member(string name); Member( Member const &); Member& operator= (Member const &); ~Member(); string getname() const; void setname(string name); void print() const; private: string myname; }; 5

Derived Class Example #include Member.h class Employee : public Member { public: Employee(string name, double money); Employee( Employee const &); Employee& operator= (Employee const &); ~Employee (); double getsalary() const; void setsalary(double money); void print() const; private: double salary; }; 6

Employee Constructor #include Employee.h Employee::Employee( string name, double money ) : Member(name) //base class initializer syntax { salary = money; } S C++ requires derived class constructor to call base class constructor to initialize inherited base class data members (if not explicit, default constructor would be called). 7

Employee s print Function void Employee::print() const { cout << Employee: ; Member::print(); //prints name from base class cout << \nsalary: << getsalary() << endl; } 8

Constructor/Destructor Order S When we instantiate a derived class: 1. Base class s member object constructors execute (if they exist) 2. Base class constructor executes 3. Derived class s member object constructors execute 4. Derived class constructor executes S Destructors called in reverse order. S Base class constructors, destructors and overloaded assignment operators are not inherited by derived classes. However derived class can call base class s version of these. 9

Encapsulation S Given a derived class can directly access and modify protected data members of base class, should base class member variables be protected? Or private? 10

Encapsulation S Given a derived class can directly access and modify protected data members of base class, should base class member variables be protected? Or private? + No overhead of function call in derived class Direct modification does not allow for error checking. If base class member variables names change, we have to change all derived classes use of them. 11

Kinds of Inheritance Base Class Access (down) Public inheritance Protected inheritance Private inheritance public public protected private protected protected protected private private private private private 12

Tidbits about Classes S Copy constructor and overloaded assignment operator (=) have to be provided when you have member variables that are dynamically allocated S Destructor also should be provided S To prevent one object from being assigned to another, declare assignment operator as private member function. S To prevent objects from being copied, make both overloaded assignment operator and copy constructor private. 13

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. cout << mptr->getname(); //what does this print? 7. mptr->print(); //and this? 14

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. cout << mptr->getname(); //Jill 7. mptr->print(); //Jill 15

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. Employee *eptr = &e1; 7. cout << eptr->getname() << eptr->getsalary(); //result? 8. eptr->print(); //what function does this call? 16

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. Employee *eptr = &e1; 7. cout << eptr->getname() << eptr->getsalary(); //Jack 65000 8. eptr->print(); //Employee.print which calls Member.print 17

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. Employee *eptr = &e1; 7. mptr = &e1; //is this ok? Base class pointer to derived class? 18

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. Employee *eptr = &e1; 7. mptr = &e1; //Yes, valid; all Employees are Members 8. eptr = &m1; //this valid? Derived class pointer to base class? 19

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. Employee *eptr = &e1; 7. mptr = &e1; //Yes, valid; all Employees are Members 8. eptr = &m1; //No, not all Members are Employees; //compiler error 20

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. mptr = &e1; //yes, this is valid; all Employees are Members 7. cout << mptr->getname(); //what does this print? 8. cout << mptr->getsalary(); //this ok? 9. mptr->print(); //what function does this call? 21

Instantiating Objects Example 1. #include Member.h 2. #include Employee.h 3. Member m1( Jill ); 4. Employee e1( Jack, 65000); 5. Member *mptr = &m1; 6. mptr = &e1; 7. cout << mptr->getname(); //Jack 8. cout << mptr->getsalary(); //compiler error 9. mptr->print(); //calls Member s print: Jack 22

23

Operator Overloading S Think of + does different things based on the types that it is applied to. S Can we apply + to objects like the Date class? S Can achieve same thing with function calls, but operator notation is often clearer and more familiar (in C++). S Can t create new operators, but can overload existing ones so they can be used with user-defined types. 24

See Example S Instead of mydate.add(otherdate), we do mydate + otherdate. Overloading + does not implicitly overload += S Write a non-static member function or global function with function name as operator<symbol> (aka operator+) S One argument of operator function must be user-defined (can t redefine meaning of operators for fundamental types) S http://www.cs.utexas.edu/users/jbsartor/cs105/code/ ArrayExample/ 25

Global vs Member Functions S Difference: member functions already have this as an argument implicitly, global has to take another parameter. S () [] -> or assignment has to be member function S Leftmost operand S For member function: must be object (or reference to object) of operator s class. S Global function used when it is not user-defined object (overloading << and >> require left operand to be ostream& and istream&) S Global operators can be made friend of class if needed. S Global functions enable commutative operations 26

Overloading Restrictions S To use an operator with class, operator must be overloaded with 3 exceptions (but these can be overloaded too): S Assignment (=) does member-wise assignment for objects. (overload for classes with pointer members) S The & and, operators may be used with objects without overloading S The following cannot be changed for operators: S Precedence S Associativity (left-to-right or right-to-left) S Arity (how many operands) S Can t overload:..* ::?: 27

Operators: Converting between Types S Conversion constructor is a single-argument constructor that turns objects of other types (including fundamental types) into objects of a particular class. S Conversion/cast operator converts object into object of another class or to a fundamental type S A::operator char *() const; //convert object of type A into char* object. const above means does not modify original object S A mya; S static_cast<char *>(mya); //CALLS mya.operator char* () S Conversion functions can be called implicitly by the compiler 28

Makefile CC = g++ -Wall Werror g testc: testcourse.o Course.o ${CC} o testc testcourse.o Course.o testcourse.o: testcourse.cpp Course.h ${CC} c testcourse.cpp Course.o: Course.cpp Course.h ${CC} c Course.cpp clean: rm rf *.o S Reusability! S Written in different language S # denotes comments S List of rule_name : dependencies <tab> command S Can do make with any rule, or by itself for 1 st rule 29

Example S Look at S http://www.cs.utexas.edu/users/jbsartor/cs105/code/ S Makefile S constdest.h S constdest.cpp S testconstdest.cpp 30

Friends of Objects S Classes sometimes need friends. S Friends are defined outside the class s scope, but are allowed to access non-public (and public) data members. S Friend functions see example S Friend classes: friend class ClassTwo; (if placed inside ClassOne definition, all ClassTwo is friend of ClassOne) S Class must explicitly declare who its friends are. 31

Friend Example #include <iostream> using namespace std; class Count { friend void setx(count &, int); public: Count() : x(0) { } void print() const { cout << x << endl; } private: int x; }; void setx( Count &c, int val ) { c.x = val; //accesses private data! } int main() { Count counter; counter.print(); setx(counter, 8); counter.print(); return 0; } 32