Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.

Slides:



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

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Constructors & Destructors Review CS 308 – Data Structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
C++ Review (3) Structs, Classes, Data Abstraction.
Chapter 10 Introduction to Classes
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
ECE122 Feb. 22, Any question on Vehicle sample code?
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Introduction to Object Oriented Programming Chapter 10.
Topics Instance variables, set and get methods Encapsulation
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Submission Example May 14, 2018
Objectives Define polymorphism Overload functions
School of EECS, Peking University
Chapter 5 Classes.
Constructor & Destructor
Constructors & Destructors
group work #hifiTeam
Introduction to Classes
Classes and Data Abstraction
Chapter 5 Function Basics
Constructors and destructors
Variables have attributes
Java Programming Language
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
Presentation transcript:

Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04

What is a constructor? It is a member function which initializes a class. A constructor has: (i) the same name as the class itself (ii) no return type 2Tahir Nawaz

Comments on constructors A constructor is called automatically whenever a new instance of a class is created. You must supply the arguments to the constructor when a new instance is created. If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body). 3Tahir Nawaz

What is a copy constructor? It is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: class_name (const class_name); It is an another way to initialize an object: you can initialize it with another object of the same type. We don’t need to create a special constructor for this; one is already built into all classes. It’s called the default copy constructor. It’s a one argument constructor whose argument is an object of the same class as the constructor. 4Tahir Nawaz

Copy Constructor Example #include // //copy constructor class test class copyconst { private: int a; public: copyconst(){ } copyconst(int x) { a=x; } void disp_sq() { cout<<"\nSquare of "<<a<<"="<<a*a<<endl; } ~copyconst() { cout<<"destructor"<<endl;} }; 5Tahir Nawaz

int main() { copyconst obj(5); copyconst obj2(obj); copyconst obj3=obj2; Copyconst obj3(8); Int a; Int b; a=b; obj.disp_sq(); obj2.disp_sq(); obj3.disp_sq(); system("PAUSE"); return 0; } 6Tahir Nawaz

Constructor Overloading Constructor overloading refers to the use of more than one constructor with same name but different number of arguments. Each constructor is called according to its signature matching Example program: #include class constructor { private: 7Tahir Nawaz

int x; public: constructor() { x=0; } constructor(int a) { x=a; } constructor(float a) { x=a; } 8Tahir Nawaz

void input(int y) { x=y; } void display() { cout<<x<<“\n”; } }; 9Tahir Nawaz

int main(void){ constructor obj1; constructor obj2(10); Constructor obj3(4.5); cout<<“After invoking constructors value of x for obj1 and obj2”; obj1.display(); obj2.display(); Obj3.display(); cout<<“After invoking input() value of x is for obj1 and obj2”; obj1.input(50); obj2.input(80); Obj3.input(50.5); obj1.display(); obj2.display(); Obj3.display(); getche(); Return 0; } 10Tahir Nawaz

Lab Task Write a program which input records of ten employees and then to print it out on the screen. The task should only be done by using constructors. Also calculate their net salary and also deduct zakat on that amount. calculate medical, rent, traveling allowances. Write a class using constructors to find the factorial of any given integer value. Tahir Nawaz11

Functions Overloading C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types. 12Tahir Nawaz

Overloaded functions are distinguished by their signatures. A signature is a combination of a function’s name and its parameter types (in order). The compiler encodes each function identifier with the number and types of its parameters. Tahir Nawaz13

Example of overloaded Function without classes #include using namespace std; int square(int x ) { cout << "square of integer " << x << " is "; return x * x; } float square( float y ) { cout << “Square of float " << y << " is "; return y * y; } int main(void) { cout << ; cout << square(7)<<endl; cout << square(7.5)<<endl; } Tahir Nawaz14

Function overloading example in a class. #include // class ovrlod { private: int a; float b,d; string c; public: void get_data(int x) { a=x; } 15Tahir Nawaz

void get_data(float y,float z) { b=y; d=z; } void get_data(string z) { c=z; } void display_sq() { cout<<"square of "<<a<<"="<<a*a<<endl; } void display_float() { cout<<"add float values "<<b<<"+"<<d<<"="<<b+d<<endl; } 16Tahir Nawaz

void display_char() { cout<<"entered char "<<c<<endl; } }; 17Tahir Nawaz

int main() { ovrlod obj; obj.get_data(5); obj.get_data(5.5,5.5); obj.get_data("Tahir"); obj.display_sq(); obj.display_float(); obj.display_char(); getch(); return 0; } 18Tahir Nawaz

static Class Data static data member – Only one copy of a variable shared by all objects of a class “Class-wide” information A property of the class shared by all instances, not a property of a specific object of the class – Declaration begins with keyword static

static Class Members static member function – Is a service of the class, not of a specific object of the class static applied to an item at file scope – That item becomes known only in that file – The static members of the class need to be available from any client code that accesses the file So we cannot declare them static in the.cpp file— we declare them static only in the.h file.

static Class Members Use static data members to save storage when a single copy of the data for all objects of a class will suffice. A class’s static data members and static member functions exist and can be used even if no objects of that class have been instantiated.

An Example of Static Class Data Here’s an example, STATDATA, that demonstrates a simple static data member: // statdata.cpp // static class data #include using namespace std; class sta { private: static int count; Tahir Nawaz22

public: sta() { count++; } int getcount() { return count; } }; // int sta::count = 0; //////////////////////////////////////////////////////////////// int main(void) { Sta f1, f2, f3; //create three objects cout << “count is “ << f1.getcount() << endl; cout << “count is “ << f2.getcount() << endl; cout << “count is “ << f3.getcount() << endl; return 0; } Tahir Nawaz23

Static Versus automatic member variables Tahir Nawaz24

//To find the area of a box #include using namespace std; class Box { private: float length, breadth, height; public: static int objCount; Box(float len=2.0, float bre=2.0, float hei=2.0) { cout <<"Constructor called." << endl; length = len; breadth = bre; height = hei; objCount++; } float Volume() { return length * breadth * height; } }; Tahir Nawaz25

int Box::objCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); cout << "Total objects: " << Box::objCount << endl; return 0; } Tahir Nawaz26

Assignment 1 Write a program in C++ by using a class to copy one string into another string and also to find out the length of the string without using any built-in function. Write a class which can convert temperatures. Such as if we provide Centigrade to Fahrenheit converter or Fahrenheit to Centigrade without using built in functions. Submission Date:18/12/2013 Time 1:00pm to 1:30pm Note: No late submission will be accepted Tahir Nawaz27