Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.

Slides:



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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Classes Separating interface from implementation
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
Learners Support Publications Functions in C++
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Object-Oriented Programming (OOP) Lecture No. 24.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Learning Objectives Pointers as dada members
Object-Oriented Programming (OOP) Lecture No. 15
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.
Review What is an object? What is a class?
Object Oriented Programming (OOP) Lecture No. 8
FUNCTIONS In C++.
Constructors & Destructors.
Review: Two Programming Paradigms
Class: Special Topics Copy Constructors Static members Friends this
This technique is Called “Divide and Conquer”.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Object Oriented Programming (OOP) Lecture No. 9
Object-Oriented Programming (OOP) Lecture No. 14
Learning Objectives Classes Constructors Principles of OOP
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Object-Oriented Programming (OOP) Lecture No. 22
Class.
Submitted By : Veenu Saini Lecturer (IT)
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Types of Computer Languages
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming (OOP) Lecture No. 10
Presentation transcript:

Object Oriented Programming (OOP) Lecture No. 8

Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier

Member Functions ► Member functions are the functions that operate on the data encapsulated in the class ► Public member functions are the interface to the class

Member Functions (contd.) ► Define member function inside the class definition OR ► Define member function outside the class definition  But they must be declared inside class definition

Function Inside Class Body class ClassName { …public: ReturnType FunctionName() { …}};

Example ► Define a class of student that has a roll number. This class should have a function that can be used to set the roll number

Example class Student{ int rollNo; public: void setRollNo(int aRollNo){ rollNo = aRollNo; }};

Function Outside Class Body class ClassName{ …public: ReturnType FunctionName(); }; ReturnType ClassName::FunctionName() {…} Scope resolution operator

Example class Student{ … int rollNo; public: void setRollNo(int aRollNo); }; void Student::setRollNo(int aRollNo){ … rollNo = aRollNo; }

Inline Functions ► Instead of calling an inline function compiler replaces the code at the function call point ► Keyword ‘inline’ is used to request compiler to make a function inline ► It is a request and not a command

Example inline int Area(int len, int hi) { return len * hi; } int main() { cout << Area(10,20); }

Inline Functions ► If we define the function inside the class body then the function is by default an inline function ► In case function is defined outside the class body then we must use the keyword ‘inline’ to make a function inline

Example class Student{ int rollNo; public: void setRollNo(int aRollNo){ … rollNo = aRollNo; }};

Example class Student{ …public: inline void setRollNo(int aRollNo); }; void Student::setRollNo(int aRollNo){ … rollNo = aRollNo; }

Example class Student{ …public: void setRollNo(int aRollNo); }; inline void Student::setRollNo(int aRollNo){ … rollNo = aRollNo; }

Example class Student{ …public: inline void setRollNo(int aRollNo); }; inline void Student::setRollNo(int aRollNo){ … rollNo = aRollNo; }

Constructor

Constructor ► Constructor is used to initialize the objects of a class ► Constructor is used to ensure that object is in well defined state at the time of creation ► Constructor is automatically called when the object is created ► Constructor are not usually called explicitly

Constructor (contd.) ► Constructor is a special function having same name as the class name ► Constructor does not have return type ► Constructors are commonly public members

Example class Student{ …public:Student(){ rollNo = 0; …}};

Example int main() { Student aStudent; /*constructor is implicitly called at this point*/ }

Default Constructor ► Constructor without any argument is called default constructor ► If we do not define a default constructor the compiler will generate a default constructor ► This compiler generated default constructor initialize the data members to their default values

Example class Student { int rollNo; char *name; float GPA; public: …//no constructors };

Example Compiler generated default constructor { rollNo = 0; GPA = 0.0; name = NULL; }

Constructor Overloading ► Constructors can have parameters ► These parameters are used to initialize the data members with user supplied data

Example class Student{ …public:Student(); Student(char * aName); Student(char * aName, int aRollNo); Student(int aRollNo, int aRollNo, float aGPA); };

Example Student::Student( int aRollNo, char * aName){ if(aRollNo < 0){ rollNo = 0; } else { rollNo = aRollNo; }…}

Example int main() { Student student1; Student student2(“Name”); Student student3(”Name”, 1); Student student4(”Name”,1,4.0); }

Constructor Overloading ► Use default parameter value to reduce the writing effort

Example Student::Student(char * aName = NULL, int aRollNo= 0, float aGPA = 0.0){ …} Is equivalent to Student(); Student(char * aName); Student(char * aName, int aRollNo); Student(char * Name, int aRollNo, float aGPA);

Copy Constructor ► Copy constructor are used when:  Initializing an object at the time of creation  When an object is passed by value to a function

Example void func1(Student student){ …} int main(){ Student studentA; Student studentB = studentA; func1(studentA);}

Copy Constructor (Syntax) Student::Student( const Student &obj){ rollNo = obj.rollNo; name = obj.name; GPA = obj.GPA; }

Shallow Copy ► When we initialize one object with another then the compiler copies state of one object to the other ► This kind of copying is called shallow copying

Example Student studentA; Student studentB = studentA; Name GPA RollNo studentA Name GPA RollNo studentB AHMAD…AHMAD… Memory

Copy Constructor (contd.) Student::Student( const Student &obj){ int len = strlen(obj.name); name = new char[len+1] strcpy(name, obj.name); … //copy rest of the data members }

Copy Constructor (contd.) ► Copy constructor is normally used to perform deep copy ► If we do not make a copy constructor then the compiler performs shallow copy

Example Name GPA RollNo A Name GPA RollNo B AHMADAHMAD Memory AHMADAHMAD Student studentA; Student studentB = studentA;