Objects as Function Arguments

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Operator overloading redefine the operations of operators
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
4/14/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 23m2 OOP Friend Functions Ref: Sebesta, Chapter 12; Lafore, Chapter 11.
1 CSC241: Object Oriented Programming Lecture No 21.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
C++ Classes & Data Abstraction
1 CSC241: Object Oriented Programming Lecture No 28.
Constructors & Destructors Review CS 308 – Data Structures.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
1 CSC241: Object Oriented Programming Lecture No 13.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
 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.
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
More C++ Features True object initialisation
Structures, Classes and Objects Handling data and objects Unit - 03.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSC241: Object Oriented Programming Lecture No 11.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
1 Introduction to Object Oriented Programming Chapter 10.
C:\Temp\Templates 4 5 Use This Main Program 6.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Visit for more Learning Resources
Chapter 14: More About Classes.
CSC241: Object Oriented Programming
Overloaded Constructors and Multiple Classes
CSC241: Object Oriented Programming
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Constructor & Destructor
Constructors & Destructors
group work #hifiTeam
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Array of objects.
Contents Introduction to Constructor Characteristics of Constructor
FUNCTIONS& FUNCTIONS OVERLOADING
Constructors and Destructors
Array of objects.
Object Oriented Programming Using C++
Constructors and Destructors
Introduction to Classes and Objects
NAME 436.
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Constructors and Deconstructor
Constructors & Destructors
The values to be assigned to the structure members are surrounded by braces and separated by commas. The first value in the list is assigned to the first.
Presentation transcript:

Objects as Function Arguments Constructors and Deconstructor The most common use of destructors is to deallocate memory that was allocated for the object by the constructor. Objects as Function Arguments our next program adds some embellishments to the ENGLOBJ example. It also demonstrates some new aspects of classes: - Constructor overloading. - defining member functions outside the class - Defining objects as function arguments. #include <iostream> class Distance //English Distance class { private: int feet; float inches; public: //constructor (no args) { } //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() //get length from user { c \ } void showdist() //display distance { \- \

void add_dist( Distance, Distance ); //declaration Constructors and Deconstructor } void add_dist( Distance, Distance ); //declaration }; { if(in { //then decrease inches inches - feet++; //increase feet ; //add the feet } int main() { //display all lengths \ \ \ cout << endl; }

value and adds to it a distance Constructors and Deconstructor value and adds to it a distance whose value is supplied by the user, to obtain the sum of the distances. It then displays all three distances: - - - -Overloaded Constructors created. That is, we would like to use definitions like ; which defines an object, width, and simultaneously initializes it to a To do this we write a constructor like this: Distance(int ft, float in) : feet(ft), inches(in) { } This sets the member data feet and inches to whatever values are passed as arguments to the constructor. However, we also want to define variables of type Distance without initializing them, as we did in ENGLOBJ. In that program there was no constructor, but our definitions worked just fine. How could they work without a constructor? Because an implicit no-argument constructor is built into the program objects, even though we -argument constructor is called the default constructor. If it class for which no constructor was defined.