220 likes | 290 Views
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.
E N D
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 <iostream> #include <string.h> // 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 <iostream> #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