C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

C++ Lecture 6 Object Life-times Creating objects using new Using pointers to objects Aggregation (Containment –UML speak) Other C++ class features.
Class and Objects.
Chapter 14: Overloading and Templates
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand the usage of classes and functions.  Understand static.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
11 Introduction to Object Oriented Programming (Continued) Cats.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
More C++ Features True object initialisation
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CONSTRUCTOR AND DESTRUCTORS
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
Abstract Data Types Programmer-created data types that specify
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
group work #hifiTeam
Introduction to Classes
Chapter 9 Objects and Classes
Constructors and destructors
Constructors and Destructors
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 9 Introduction To Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS3 C++ Classes.
Presentation transcript:

C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and destructor functions Constant functions Inline functions Microsoft Visual C++ demonstration

Function overloading In C++ it is possible to have several functions all with the same name provided that they differ in the number and/or type of their arguments. The compiler is able to select the correct function to call based on the number and type of the arguments used in the actual function call. A difference in the return type is not used to differentiate between functions Function overloading can be used for any functions but is often used in constructor functions.

Function overloading void func(int x) { cout << x << “ squared is “ << x * x << endl; } void func (int x, int y) { if (x>y) cout << x << “is biggest” << endl; else cout << y << “is biggest” << endl; } func(42); // would call first function func(22,44); //would call second function

Default function arguments Normally when a function is called all the arguments that are required by the function must be present. In C++ the function can be made to use a default value is the argument is not provided when the function is called. There are some restrictions Default arguments should be used sparingly Default arguments are often used with constructor functions in classes

Default function arguments A default value is assigned in the function declaration only (not the function definition) – E.g. void car (int capacity, int cylinders = 4, int seats = 5); – Now the function can be called :- the normal way :- car(2000, 6, 4); // override default values or car(2000); provide first argument and use the default value for the others or car(3000,6); provide first 2 arguments and use the default value for the third but NOT car(3000,,3); // NOT ALLOWED!!

Thinking about objects What are the attributes/properties of the object – The attributes become the data members of the class – These are normally placed in the private section of the class – Private data is only accessible only by class member functions What actions and behaviours does the object have – The actions/behaviours become the member functions of the class – The member functions are normally placed in the public section of the class

Member functions There are 3 types of member functions that we can identify – mutator : these modify the member data – accessor : these access the member data but do not change it i.e. read only access – manager : provide house-keeping for the class itself To allow users of the class to access the private data in a controlled way the following types of member functions are often provided in a class set_memberdata(data_type a_value); // A mutator function data_type get_memberdata(); // An accessor function

Member functions A class member function is placed in the private section if it is only used internally by the class and the users of the class should not be allowed to use the function directly Data can be placed in the public section of the class. This however is poor programming practice and goes against the idea of data hiding and encapsulation.

Constructors and destructors At present when an object is created (i.e. an instance of a class) the member variables will have random values. It is preferred to have objects created in a particular known state. Constructor functions are used. Constructor functions are automatically called when an object needs to be created. When an object goes out of scope and is no longer required a destructor function is automatically called to perform any “clean-up” that may be required.

Constructors A constructor function has the same name as the class. The constructor has no return type not even void. The constructor may take zero or more arguments A constructor requiring NO arguments is called the default constructor. The constructor function is often overloaded The constructor may often use default arguments. If a constructor is not provided a default constructor is provided by the compiler that does nothing. When a copy of an existing object is required (e.g. passing an object by value to a function) a copy constructor is invoked. If no copy constructor is provided the compiler provides one that does a member-wise copy of all the member variables. (Often this is sufficient)

Destructors The destructor function has the same as the class but preceded by the tilde ‘~’ character The destructor has no return type The destructor takes no arguments Therefore destructor cannot be overloaded

Copy Constructor A copy constructor is automatically called when an object needs to be copied. E.g. Consider passing objects to functions:- someclass x; // x is an instance of someclass func(x); // call func passing the object x //The function func void func(someclass f_arg) { // do something with f_arg } – This is pass by value i.e. f_arg is a copy of x – The compiler will automatically construct f_arg and call the copy constructor to copy the data member variables of x into f_arg We will look at copy constructor functions later.

Example class – cpatient.h #if ! defined cpatient_h #define cpatient_h class CPatient { public: CPatient(const char *n, int a, char g); CPatient();// default constructor ~CPatient(); void set_name(char * n); void set_age(int a); void set_gender(char g); void identify(); // display all member variables const char * get_name(); int get_age(); char get_gender(); private: char name[30]; int age; char gender; }; #endif

cpatient.cpp #include “cpatient.h” #include #include // old c string functions using namespace std; CPatient:: CPatient(const char *n, int a, char g)// Normal constructor { //cout << “constructing patient” << endl; strcpy(name,n); age = a; gender = g; } CPatient:: CPatient() //default constructor – no arguments { //cout << “default constructor” << endl; strcpy(name,”A.N.Other”); age = 0; gender = ‘M’; }

cpatient.cpp (cont.) CPatient:: ~CPatient() { } //destructor – does nothing void CPatient:: identify() { cout << "Name : " << name << endl; cout << "Age : " << age << endl; cout << "Gender : " << gender << endl << endl ; } void CPatient:: set_name(char * n) { strcpy(name,n); } void CPatient:: set_age(int a) { age = a; } void CPatient:: set_gender(char g) { gender = g; }

cpatient.cpp (cont.) const char * CPatient:: get_name() { return name; } int CPatient:: get_age() { return age; } char CPatient:: get_gender() { return gender; }

main.cpp #include #include “cpatient.h” using namespace std; cpatient x;//global cpatient object void main() { CPatient a(“Arthur”, 42, ‘M’);// create local cpatient object a.identify(); x.identify(); a.set_age(34); a.set_name(“Arthur Dent”); a.identify(); char g = a.get_gender(); if (g == ‘M’) cout << “Mr. ”; else cout << “Ms.”; cout << a.get_name(); // etc. }

More class features – constant functions If a class member function does not modify any of the class member variables (i.e. an accessor function) then it is good programming practice to make it a constant member function by adding the keyword const in both the function declaration and the function definition. Member functions identify, get_name, get_age and get_gender are accessor funtions E.g. modifications to identify function declaration - void identify() const; definition - void CPatient::identify() const {

More class features – inline functions inline function – Compiler replaces function call with actual code of the function body – Can produce larger programs if function has many statements – Can be more efficient if function is a very simple one line statement – avoids stacking and unstacking of function arguments and therefore produce faster execution functions that are defined in the class definition are implicitly inline Any function can be explicitly made an inline function by preceding the function with the inline keyword

Example use of inline keyword class CAny { public: void func1( );// normal function call void func2( ) { x = 2; } // implicitly inline inline void func3( ); //explicitly inline private: int x; }; CPP File void CAny:: func1() { x = 1; } inline CAny::func3() { x = 3; } Program code CAny b; b.func1( );// calls the member function func1 b.func2( );// compiler places the statement to modify x inline b.func3( ); // compiler places the statement to modify x inline

Summary Function Overloading - constructors Default Functions arguments - constructors C++ class - Abstraction & Encapsulation Construction of objects Constant functions – defensive programming Inline functions

Microsoft Visual C++ Support for C++ classes Demonstration Example programs