Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.

Slides:



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

Object Oriented Programming
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structure.
Structures Spring 2013Programming and Data Structure1.
Classes and Objects Presented by: Gunjan Chhabra.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
C++ fundamentals.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
IT PUTS THE ++ IN C++ Object Oriented Programming.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Welcome to Classes and Objects Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )TGT(CS) KV JHAGRAKHAND.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Procedural and Object-Oriented Programming
Classes C++ representation of an object
2 Chapter Classes & Objects.
Class and Objects UNIT II.
Review: Two Programming Paradigms
This technique is Called “Divide and Conquer”.
Object-Oriented Programming Using C++
Lecture 4-7 Classes and Objects
Object Oriented Analysis and Design
Lecture 8 – 9 Arrays with in a class
CS212: Object Oriented Analysis and Design
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object-Oriented Programming
Object oriented programming (OOP) Lecture No. 7
Object-Oriented Programming
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
Object Oriented Programming (OOP) Lecture No. 12
Classes and Objects Systems Programming.
C++ Object Oriented 1.
Introduction to Classes and Objects
Presentation transcript:

Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming Advantages of OOPs 1.Deals with real life objects, so provide ease in programming. 2. Provides means for handling data security etc. 3. Promotes reusability of code, thus programming and testing becomes easy and fast.

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class Member Functions Data members keyword Class name(user defined Data type) emp e1; //object (class variable)

Access Specifiers A class has 3 levels of visibility mode. 1. Public: Public members are accessible outside the class Public members are accessible to both member Functions and non-member Functions. Objects can access the members directly using dot operator. e.g object name.public data member object name.public member function

class emp { int empno; char empname[20]; float basic; public: char address[20]; void readdata() {cin>>empno; gets(empname); gets(address); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic<<address; } };// end of class emp e1; Outside the class we can access e1.address //OK Object name.public data member e1.readdata() //OK Object name.public member function e1.displaydata()//OK Object name.public member function

Public members are accessible outside the class void main() { cin>>e1.address //OK Object name.public data member e1.readdata() //OK Object name.public member function } class emp { int empno; char empname[20]; float basic; public: char address[20]; void readdata() {cin>>empno; gets(empname); gets(address); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic<<address; } };// end of class

class emp { int empno; char empname[20]; float basic; public: char address[20]; void readdata() {cin>>empno; gets(empname); gets(address); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic<<address; } };// end of class Public members are accessible to member Functions

Objects can access the public data members and member functions directly using dot operator. void main() { cin>>e1.address //OK Object name.public data member e1.readdata() //OK Object name.public member function } class emp { int empno; char empname[20]; float basic; public: char address[20]; void readdata() {cin>>empno; gets(empname); gets(address); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic<<address; } };// end of class

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class void main() { emp e1; //To input details cin>>e1.empno; //NOT OK ? e1.readdata(); //OK //To Display details e1.displaydata();// OK } WHY ?

Private Visibility mode Private members can be accessed only by a member function of that class They cannot be accessed by any non member function (data is hidden from outside world) Object of a class cannot access private members using dot operator. All data at the beginning of a class by default is private until any specifier is mentioned.

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class Private members can be accessed only by a member function of that class

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class void main() { emp e1; e1.empno; //NOT OK } Object of a class cannot access private members using dot operator.

All data at the beginning of a class by default is private until any specifier is mentioned. class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class

Member Functions They define the set of operations that may be applied to objects of that class. In a way these functions are used to define the behaviour of Objects of the class and the way the data members will interact with other functions of the same or another class. class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class

Member Function Declaration Inside the class If the member functions are defined inside the class then there is no need for function prototype class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class

Outside the class class emp { int empno; char empname[20]; float basic; public: void readdata(); void displaydata(); };// end of class Function Prototype Larger functions are defined outside the class, it is done with scope Resolution operator. It requires Function prototype inside the class.

void emp :: displaydata() {cin>>empno; gets(empname); cin>>basic; } void emp :: readdata() {cout<<empno; cout<<empname; cout<<basic; } return_data_type class_name :: function_name() - syntax Function Definition Outside the class

Nesting of Member Function A member function of a class can be called by another member function Of the same class. takedata() is calling display() class emp { float basic; void display() { cout<<basic; } public: void takedata() { cin>>basic; display(); } }; void main() { emp e1; e1.takedata(); }

Arrays within a class Arrays can be used a data members in a class. If an array is declared as private, then only member functions can access it. If it is public, can be accessed outside the class also. class company { public: int no_of_emp[100]; int no_of_dept[30]; private: int no_of_units[20]; };

Arrays of Objects We can have arrays of class objects. class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class void main() { emp e[100]; //ARRAY of objects for(int I=0;I<100;I++) { e[I].readdata(); e[I].displaydata(); }

Memory Allocation of Objects When a class is defined, memory is allocated for its Member functions and they are stored in the memory Once. When an object is created, separate memory space is allocated for its data members. All objects work with one copy of member functions Shared by all.

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno; gets(empname); cin>>basic; } void displaydata() {cout<<empno; cout<<empname; cout<<basic; } };// end of class class emp member functions readdata( ) displaydata( ) Memory created when Member functions are defined emp e1,e2; e1 e2 empno empname basic empno empname basic Memory created when objects are declared

Passing Objects as Function Arguments By value A copy of the entire object is passed as an argument to a Function any modification made to the object inside the Function is not reflected in the object itself.

class emp { int empno; char empname[20]; float basic; public: void read() {cin>>empno;gets(empname); cin>>basic;} void disp () {cout<<empno;cout<<empname; cout<<basic;} void mod(emp empl1)//by value {empl1.empno=10; } };// end of class void main() { emp e1,e2; e1.read (); e2.read(); e1.mod(e2); e2.disp(); }

class emp { int empno; char empname[20]; float basic; public: void readdata() {cin>>empno>>basic; gets(empname);} void displaydata() {cout<<empno<<basic; cout<<empname;} void modify(emp temp)//By Value { temp.empno=2; } };// end of class void main() { emp e1; e1.readdata( ); emp e2; e2.readdata( ); e2.modify(e1); e1.displaydata(); e2.displaydata( ); }

Passing Objects as Function Arguments By Reference A copy of the object is not passed as an argument rather the Member Function works directly on the actual object used In the call. Any modification made to the object inside the Function is reflected in the object itself.

class emp { int empno; char empname[20]; float basic; public: void read() {cin>>empno;gets(empname); cin>>basic;} void disp () {cout<<empno;cout<<empname; cout<<basic;} void mod(emp &empl1)//by reference {empl1.empno=10; } };// end of class void main() { emp e1,e2; e1.read (); e2.read(); e1.mod(e2); e2.disp(); }

class dist { float feet,inches; public: void read() {cin>>feet>>inches; } void disp () {cout<<feet<<inches;} dist sum(dist); };// end of class void main() { dist l1,l2; l1.read();l2.read(); dist l3=l1.sum(l2); l3.disp(); } dist dist::sum(dist d2) { dist d3; d3.feet=feet+2d.feet+(inches+d2.inches)/12; d3.inches=(inches+d2.inches)%12; return d3; }

Function returning objects: Function can also return objects