Overloaded Constructors and Multiple Classes

Slides:



Advertisements
Similar presentations
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Advertisements

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.
1 CSC241: Object Oriented Programming Lecture No 13.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
1 CSC241: Object Oriented Programming Lecture No 16.
Structures, Classes and Objects Handling data and objects Unit - 03.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
1 CSC241: Object Oriented Programming Lecture No 05.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
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.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to Object Oriented Programming ( P ) Malik Jahan Khan 1.
Ref: Sebesta, Chapter 12; Lafore, Chapter 11
Fucntions in C++ Malik Jahan Khan
Visit for more Learning Resources
Class and Objects UNIT II.
LESSON 2 Basic of C++.
CSC241: Object Oriented Programming
C++ Programming Functions
CSC241: Object Oriented Programming
CONSTRUCTORS & DESTRUCTORS
CSC241: Object Oriented Programming
Command Line Arguments
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
School of EECS, Peking University
CS1201: Programming Language 2
Presented By: Nazia Hossain Lecturer, Stamford University
this Pointer Scope Resolution Operator Friend Function
group work #hifiTeam
Lecture Constructors and Destructors
Lecture 4-7 Classes and Objects
Lecture 8 – 9 Arrays with in a class
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Dynamic Memory Allocation Reference Variables
Contents Introduction to Constructor Characteristics of Constructor
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Inheritance.
Pointers & Functions.
Classes Short Review of Topics already covered Constructor
More ‘concepts’ on Function
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Object Oriented Programming Using C++
Operator Overloading.
CHAPTER 2 Arrays and Vectors.
CS1201: Programming Language 2
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CHAPTER 2 Arrays and Vectors.
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Class: Special Topics Overloading (methods) Copy Constructors
TOPIC: FUNCTION OVERLOADING
Introduction to Algorithms and Programming COMP151
Objects as Function Arguments
Constructors & Destructors
OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class
CS 144 Advanced C++ Programming April 30 Class Meeting
More ‘concepts’ on Function
Functions Chapter No. 5.
Presentation transcript:

Overloaded Constructors and Multiple Classes OOP Overloaded Constructors and Multiple Classes Malik Jahan Khan Malik Jahan Khan

OOP Overloaded Functions We may have many functions in the same program having same function name but different number of arguments They are differentiated by the number of arguments passed while calling them from main() Such functions are called overloaded functions Malik Jahan Khan

Constructors having Arguments Some times, we need to specify default initialization values at the time of object creation This can be done by passing arguments to constructors In this way, each object may be initialized through constructor with a different value

Example #include<iostream> using namespace std; class Person { private: int id; public: Person(int d) : id(d) } void showID() cout<<"\n My ID is "<<id; }; int main() Person p2(10),p1(20),p3(30); p1.showID(); p2.showID(); p3.showID(); Output My ID is 20 My ID is 10 My ID is 30

Overloaded Constructors There may be many constructors of a class, each having different number of arguments

Example Output Feet = 1.... Inches = 5 Feet = 5.... Inches = 9 #include<iostream> using namespace std; class Distance { private: int feet; float inches; public: Distance(): feet(1), inches(5.0) } Distance(int ft, float in): feet(ft), inches(in) void showDist() cout<<"\n Feet = "<<feet<<".... Inches = "<<inches; }; void main() Distance d1; Distance d2(5,9); d1.showDist(); d2.showDist(); Output Feet = 1.... Inches = 5 Feet = 5.... Inches = 9

Default Copy Constructor A no-argument constructor initializes data members with constant values A multi-argument constructor initializes data members to values passed as arguments We can also initialize an object with another object of same class Surprisingly, we don’t need to write a special constructor for it It is already built into all classes and known as “default copy constructor”

Example Output dist1 = 11'-6.25" dist2 = 11'-6.25" dist3 = 11'-6.25” #include <iostream> using namespace std; class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in): feet(ft), inches(in) void showdist() { cout << feet << "\'-" << inches << "\""; } }; int main() Distance dist1(11, 6.25); //two-arg constructor Distance dist2(dist1); //one-arg constructor Distance dist3 = dist1; //default copy constructor cout << "\ndist1 = "; dist1.showdist(); cout << "\ndist2 = "; dist2.showdist(); cout << "\ndist3 = "; dist3.showdist(); cout << endl; return 0; } Output dist1 = 11'-6.25" dist2 = 11'-6.25" dist3 = 11'-6.25”

Multiple Classes In real world scenarios, we may have deal with different classes in a single problem In an OO program, we may define as many classes as we need to represent a real world problem

Example OOP #include <iostream> #include<stdio.h> #include<cstring> using namespace std; class Car { public: int number; char color[10]; Car() { } Car(int n, char col[10]): number(n) { strcpy(color,col); } }; class Person private: int id; char name[10]; Car myCar; Person(): id(0) void setData(int i, char n[10], Car c) { id=i; strcpy(name,n); myCar=c; } void showData() cout<<"\n I am ID No. "<<id<<", my name is "<<name<<". I have car number "<<myCar.number<<" of color "<<myCar.color; }; int main() Person p1, p2, p3; Car c1(3439, "Red"),c2(7777, "Black"); p1.setData(1,“Tahira",c1); p2.setData(2,“Maryam",c2); p3.setData(3,“Kainat",c1); p1.showData(); p2.showData(); p3.showData(); cout<<"\n"; return 0; Malik Jahan Khan

Output I am ID No. 1, my name is Tahira. I have car number 3439 of color Red I am ID No. 2, my name is Maryam. I have car number 7777 of color Black I am ID No. 3, my name is Kainat. I have car number 3439 of color Red