Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Learners Support Publications Classes and Objects.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Chapter 10 Introduction to Classes
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: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Function Overloading and References
Chapter -6 Polymorphism
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
C++ Functions A bit of review (things we’ve covered so far)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Chapter 15 - C++ As A "Better C"
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CSE1002 – Problem Solving with Object Oriented Programming
C++ Lesson 1.
Programming with ANSI C ++
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?
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.
7. Inheritance and Polymorphism
Programming with ANSI C ++
Programming with ANSI C ++
FUNCTIONS In C++.
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?
More about OOP and ADTs Classes
CS212: Object Oriented Analysis and Design
Templates.
CISC/CMPE320 - Prof. McLeod
Reserved Words.
Polymorphism Lec
Programming with ANSI C ++
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
More about OOP and ADTs Classes
Introduction to Programming
Classes and Objects.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Class.
Submitted By : Veenu Saini Lecturer (IT)
C++ Programming CLASS This pointer Static Class Friend Class
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Presentation transcript:

Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Chapter 4 Functions

Similarity and difference with C functions The function calling, execution and return of a C++ function is technically similar to that of a C function. C++ has inline functions when programmer would like to eliminate the overhead of context switching Also, the object code of the C++ function contains some additional information then a C function. Main is different here

Inline functions provide the flexibility of normal functions and provide efficiency of macros precede the function name with keyword inline at the time of definition must be defined before usage Eliminate the context switching overhead Increase the program size It is a request

Default arguments bool CheckPass(int TotalMarks, int PassingMarks = 50); CheckPass(int Sub1Marks, int Sub2Marks, int PassingMarks = 50) CheckPass(int Sub1Marks, int Sub2Marks, int PassingMarks1 = 50, int PassingMark2 = 35); // following is wrong! CheckPass(int Sub1Marks, int PassingMarks1 = 50, int Sub2Marks, int PassingMark2 = 35);

Objects as parameters class Time { } Time Difference(Time , Time); Temporary object creation and NRV optimization

Call by and return reference Time AddTimes(Time & , Time & ); StudList.GetStudent(OriginalName) = NewNode; Returning a temporary object as an alias of existing variable

Prototype and overloading A function prototype MyFunction(int First, char Second) Overloading a funtion int Add(int, int); double Add(double, double); string Add(string, string); Overloading does not involve return type

Other issues Function overloading and polymorphism Use and differences between default argument and function overloading solutions Program Readability and Default arguments

Member and non member We need to pass only one argument in place of two arguments while using a member function. In the non member case, Difference (Time1, Time2) or Difference (Time2, Time1) can be possible. Non member need either public members or be friends We do not need an explicit return statement while using a member function.

Non friend non member function The class is not cluttered with unnecessary member functions and remains manageable. The function code is not affected if private variables (the implementation of the class) changes, as the function are not using any private variables

A friend function friend SomeFunction(list of arguments). friend void ListDeptWise(employee[]); A function is not truly a member by nature but needs access to private members should not be made member but a friend Friends violate information hiding principle

Other issues Const and Volatile functions void PrintDetails() const { ….} void CheckValuesForNIC() volatile {…} Mutable data members

Static functions static int TotalStudents; static void DispTotal();// a declaration void student :: DispTotal() // definition { cout << "Total Students at the moment "<< student::TotalStudents<< "\n"; } a static function can only have static variables of the class to act upon,

Static and normal functions They do not possess this pointer. They can not access other data members of the class. They can not be virtual. They can not be declared either const or volatile.

Returning an object employee GetManager(employee EmpArray[]) { … return EmpArray[i]; }} employee GetEmp(string EmpName, employee EmpArray[]) for (int i=0;i<10;++i) if (EmpArray[i].Name==EmpName) return EmpArray[i]; } }

Public and Private functions class employee { … bool IsPermanent() /* a private function */ return (EmpNo > 2000); }

Non member function pointers int (*PointFun)(int,int); // defining pointer to a function which has two arguments, both of them are integers Two ways to call the function cout << (*PointFun) (PointArg1,PointArg2); cout << PointFun(PointArg1,PointArg2);

Member function pointers int PointFunEx :: PointAccess() int (PointFunEx::*PA)(); //a function pointer PA = PointFunEx :: PointAccess; cout << " " <<(PFE.*PA) (); int (PointFunEx::*PB)(); /*a function pointer PB is defined here */ PB = &PointFunEx :: PointAccess;

Operators <class name > :: for defining pointer to function and making a call. ::* operator to define pointer to a member function .* operator for accessing a member pointer using an object ->* operator for accessing a member pointer using pointer to an object.

Adding C function to a C program C++ ‘decorates’ the compiled function to represent its arguments and types of them. at run time, there is no overhead extern "C" void TestCLinkage(); extern "C" { void TestCLinkage(); void TestCLinkage2(); }