MY COLLEGE: BITM SANTINIKETAN Object oriented programming lets check through C++

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
C++ fundamentals.
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.
OOP Languages: Java vs C++
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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
Welcome to Classes and Objects Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )TGT(CS) KV JHAGRAKHAND.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 CSC241: Object Oriented Programming Lecture No 02.
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.
CONSTRUCTOR AND DESTRUCTORS
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 CSC241: Object Oriented Programming Lecture No 03.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
1 Introduction to Object Oriented Programming Chapter 10.
Object-Oriented Programming (OOP) and C++
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
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.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Constructors and Destructors
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Procedural and Object-Oriented Programming
Static data members Constructors and Destructors
By Muhammad Waris Zargar
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Concepts and Basics of C++ Programming
This technique is Called “Divide and Conquer”.
Concepts and Basics of C++ Programming
Object Oriented Analysis and Design
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
CONSTRUCTORS AND DESRUCTORS
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Presentation transcript:

MY COLLEGE: BITM SANTINIKETAN

Object oriented programming lets check through C++

GLIMPSES OF OOP OBJECT ORIENTATION WHAT IS AN OBJECT?? AN OBJECT IS A PERSON,PLACE,ACTIVITY,ITEM OR ANY ENTITY. SO,U,ME,ATABLE.CAKE AND EVEN AN AMOEBA IS ENTITY. SO,U,ME,ATABLE.CAKE AND EVEN AN AMOEBA IS AN OBJECT.HENCE WE CAN SAY DAT THE WORLD IS FULL OF OBJECTS.NOW THE QUESTION LIES HERE THAT AFTERALL HOW DO WE DIFFER OR RATHER IDENTIFY THESE OBJECTS….

YA,THE ANSWER IS AS SIMPLE AS EATING A CHOCOLATE !!!! WE SIMPLY CLASSIFY THEM IN DIFEERENT GROUPS ON THE BASIS OF SOME COMMON FEATURES OR SIMILARITIES…

EXAMPLES OF OBJECTS:

CLASSIFICATION OF OBJECTS GENERAL PROPERTIES OF AN OBJECT: 1.> IDENTITY: IT DEPICTS THE IDENTIFICATION OF AN OBJECT i.e. WHAT OR WHO IS IT? E.g.A TELEVISION SET 2.> BEHAVIOUR: WHAT DOES IT DO…OR RESONSE TO ANY STIMULI..E.g A T.V SET SHOWS PICTURE ON GIVING CURRENT. 3.> STATE : E.g it is on or off……… IT DEPICTS CURRENT SITUATION. SO,SEEING THE COMMON PROPERTIES,WE CAN CLASSIFY COMMON OBJECTS INTO CORRESPONDING GROUPS CALLED CLASS… E.g. TABLE,CHAIR,BENCHES ARE OBJECTS WHICH COME UNDER CLASS FURNITURE… TEACHER,PEON,DIRECTOR ARE OBJECTS OF CLASS EMPLOYEE OF ANY COLLEGE…. WE CAN SAY THAT TEACHER IS AN INSTANCE OF CLASS EMPLOYEE…. TEACHER,PEON,DIRECTOR ARE OBJECTS OF CLASS EMPLOYEE OF ANY COLLEGE…. WE CAN SAY THAT TEACHER IS AN INSTANCE OF CLASS EMPLOYEE….

EXAMPLE OF CLASS FORMATION

TALKING MORE ABOUT CLASS-> SO,WE COME TO KNOW WHAT A CLASS IS… WE CAN SAY DAT CLASS IS AN ABSTRACT DATA TYPE… THUS WE CAN SAY THAT CLASS IS A COLLECTION OF SIMILAR OBJECTS… NOW IF WE TALK ABOUT A CLASS EMPLOYEE WITH OBJECT TEACHER,THEN THE TEACHER WILL HAVE A NAME,SALARY,DESIGNATION ETC….THESE FIELDS ARE CALLED DATA ITEMS FOR OBJECT TEACHER OF CLASS EMPLOYEE.. WE NEED TO ASSIGN VALUES TO THESE DATA ITEMS INORDER TO DEFINE OR DESCRIBE THAT OBJECT… HENCE A OR MORE FUNCTIONS ARE NEEDED TO DEFINE THESE DATA ITEMS… SO,ULTIMATELY WE SEE THAT EACH CLASS CONTAINS SOME DATA MEMBERS AND FUNCTIONS..

THIS PROPERTY OF COMBINING DATAS AND FUNCTIONS INTO A SEPARATE ENTITY CALLED CLASS IS KNOWN AS ENCAPSULATION..

LIFE BLOOD RELATIONSHIP HENCE WE COME TO THE CONCLUSION THAT AN OBJECT IS AN INSTANCE OR DESCRIPTIVE AGENT OF ANY CLASS.. ICE+CREAM=ICE CREAM OBJECT+CLASS=00P DATAS+FUNCTIONS=CLASS

ILLUSTRATION NOW,WE WILL DISPLAY A CLASS EMPLOYEE WITH DATA MEMBERS: NAME,SALARY,DESIGN,ACCEPT( ),DISPLAY( ) -  class employee { char name[20]; char name[20]; float sal; float sal; char des[20]; char des[20]; void accept( ); void accept( ); void display( ); void display( ); }; }; THUS WE SEE THE SYNTAX OF CLASS DECLARATION AS: class class { data members; data members; functions; functions;}; AND OBJECT DECLARATION OF THIS CLASS AS: Employee obj;

MORE METHODS OF OBJECT DECLARATION METHOD 1: Class employee { ; ; }obj; METHOD 2: Class employee { ; ; }; Employee obj;

MORE ABOUT CLASS NOTE: THE TERM CLASS IS A KEYWORD… SO BEWARE WHILE USING IT!! A COMPLETE DECLARATION OF A CLASS:: class play { private: private: char name[20]; char name[20]; float price; float price; public: public: void read(); void read(); void display(); void display();}; Play p; // object declaration of class………… We see the terms private and public here… these are known as ACCESS SPECIFIER.. THE ACCESS SPECIFIERS DENOTE THE ACCESS OR REACHABILITY LIMIT OF THE MEMBER OF A CLASS…

ACCESS SPECIFIER:LETS SPECIFY THE THREE TYPES OF ACCESS SPECIFIERS USED IN OOP ARE::>> 1.. PRIVATE 2.. PUBLIC 3.. PROTECTED THE MEMBERS OF A CLASS ARE PRIVATE IN THE SENSE THAT THEY CANNOT BE ACCESSED OUTSIDE THR CLASS IN WHICH THEY ARE DECLARED.. MEANWHILE,OBJECTS OF A CLASS COMMUNICATES WITH EACH OTHER OR OTHER OBJECTS (i.e OUTSIDE WORLD) THROUGH PUBLIC INTERFACE…THE MEMBERS DECLARED AS PUBLIC CAN BE ACCESSED EVEN OUTSIDE THE CLASS THROUGH OBJECTS.. PROTECTED MEANS THAT THE MEMBER DATAS & FUNCTIONS CAN BE ACCESSED ONLY IN THE CLASS IN WHICH THEY HAVE BEEN DECLARED THROUGH USE OF OBJECTS.. NOTE:: A MEMBER IF NOT BEEN GIVEN ANY ACCESS SPECIFICATION IS BY DEFAULT PRIVATE…….

MORE ABOUT ACCESS SPECIFIER & FUNCTION DEFINITION IN OOP THIS PROPERTY OF HIDING ESSENTIAL DATAS AND FUNCTIONS OF A CLASS FROM D OUTSIDE WORLD IS KNOWN AS DATA OR INFORMATION HIDING.. ONE OR MORE OBJECTS OF A CLASS CAN COMMUNICATE WITH EACH OTHER USING THE PUBLIC INTERFACE..THUS ESSENTIAL FEATURES ARE SHARED WITH THE OUTSIDE WORLD THROUGH PUBLIC SPECIFIER.. THIS PROPERTY IS KNOWN AS DATA ABSTRACTION.. e.g in a milk vending machine,d cream is kept by the milkman for his home use and milk for selling..the 1 st work is data hiding and 2 nd is data abstraction.. In general practice,data members are declared as private and fuction members as public members…

DECLARATION OF A CLASS ALONG WITH FUNCTION DEFINITION LET US SEE THE FOLLOWING CLASS:: Class play { private: private: char name[20]; char name[20]; float price; float price; public: public: void input(); void input(); }; }; Void play::input() { cout<<“entervname of item”: cout<<“entervname of item”: cin>>name; cin>>name; cout<<“enter price \n “; cout<<“enter price \n “; cin>>price; cin>>price;} NOTE: FUNCTION DEFINITION IS AS:: void play::input()  SYNTAX: :: ::

USE OF DOUBLE SCOPE RESOLUTION OPERATOR 1.HERE :: IS DOUBLE SCOPE RESOLUTION OPERATOR.. IT HELPS TO KNOW THAT TO WHICH CLASS THE FUNCTION BELONGS TO.. IT REMOVES AMBIGUITY. 2. IT IS USED BEFORE ANY VARIABLE TO DECLARE IT AS GLOBAL VARIABLE..

ANOTHER METHOD OF FUNCTION DEFINITION IF THE FUNCTION BODY IS VERY SMALL,IT CAN BE DEFINED INSIDE THE CLASS BODY ONLY AS::: Class play { private: private: char name[20]; char name[20]; float price; float price; public: public: void input( ) void input( ) { cout<<“ enter name \n”; cout<<“ enter name \n”; cin>>name; cin>>name; cout<<“ enter price\n”; cout<<“ enter price\n”; cin>> price; cin>> price; }};

> > AFTERALL COMPLETION OF A TASK MATTERS.. SO THIS ONE IS FOR ALL OF U.. CLASS PLAY: ->NAME,PRICE INPUT( ); DISPLAY( ); ->OBJECT IS P

#include<iostream.h>#include<conio.h> Class play { private: char name[20]; float price; public: void input( ); void display( ); }; Void play::input( ) { cout<<“enter name and price”; cout<<“enter name and price”; cin>>name>>price; cin>>name>>price;} Void play::display( ) { cout<<“name and price is \n”<<name<<price; cout<<“name and price is \n”<<name<<price;}

Void main( ) { play p; // object of class play p; // object of class p.input( ); // calling of function p.input( ); // calling of function p.display(); p.display(); getch( ); getch( );}

CONSTRUCTOR & DESTRUCTOR CONSTRUCTOR is the first function to be called in an oop. talking about the object and class declaration,whenever an object is declared,some memory space is allocated for it… the function which allocates memory space to the object( members of class) is known as constructor.

Whenever a program is executed for compilation,memory space is allocated for each data items.this is the instance when constructor is automatically called for allocating space to the object.. Even it is not present in the instruction code,it is by default called by the compiler..

HOW TO DEFINE A CONSTRUCTOR ? A CONSTRUCTOR CAN BE DECLARED AS OTHER FUNCTIONS ARE DONE IN A CLASS. BUT THE BASIC DIFFERENCE IS THAT THE NAME OF CONSTRUCTOR IS SAME AS THAT OF A CLASS.. A CONSTRUCTOR WITH NO BODY can be declared as: Class play { private: private: int m; int m; public: public: play( ) play( ) { } void input( ); void input( );};

The point to be noted is that constructor does not have any return type. The other major use of constructor is to initialize the Data members of class.. i.e in the following code to find factorial of any number,the variable fact should be initialized to 1.. class play { int num; int num; int fact; int fact; public: public: play( ) play( ) { fact=1; /* use of const. to initialise data member which is later used in the program.*/ fact=1; /* use of const. to initialise data member which is later used in the program.*/ } int factorial(int ); int factorial(int );};

TYPES OF CONSTRUCTOR Constructors are basically of three types:  1. DEFAULT CONSTRUCTOR 2. PARAMETERIZED CONSTRUCTOR 3. COPY CONSTRUCTOR Default constructor was the one we had used in the last illustration shown. It has no argument or parameter list.. It can or cannot have a body.. ILLUSTRATION: class play { int num; int num; int fact; int fact; public: public: play( ) play( ) { fact=1; /* use of const. to initialise data member which is later used in the program.*/ fact=1; /* use of const. to initialise data member which is later used in the program.*/ } int factorial(int ); int factorial(int );};

PARAMETERIZED CONSTRUCTOR A parameterized const. is the one which has some parameter lists.. When we need to pass values of some data members of class from the main program i.e from the user, we need such constructor.. THE NEXT CODE WILL SPECIFY THE USE OF PARAMETERIZED CONSTRUCTOR..

PASSING PARAMETERS

NOW SUPPOSE WE HAVE TO ACCEPT ACCOUNT NO. AND BALANCE FROM THE USER AND DISPLATY THROUGH FUNCTION OF OBJECT. A CLASS “BANK” IS USED WITH PARAMETERIZED CONSTRUCTOR “BANK” PARAMETERIZED CONSTRUCTOR “BANK” ACCESS MODE DATA MEMBER PRIVATEACC_NO BALANCE PUBLIC BANK(ACC _NUM,BAL ) DISPLAY( )

#include<iostream.h>#include<conio.h> Class bank { int acc_no; int acc_no; int balance; int balance; public: public: bank(int acc_num,int bal) bank(int acc_num,int bal) { acc_no=acc_num; acc_no=acc_num; balance=bal; balance=bal; } void display( ); void display( );}; Void bank::display( ) { cout<<“ACCOUNT NO.”<<acc_no; cout<<“ACCOUNT NO.”<<acc_no; cout<<“BALANCE IS “<<balance; cout<<“BALANCE IS “<<balance;} Void main( ) { bank b; bank b; bank(2345,500); bank(2345,500); b.display( ); b.display( ); getch( );} getch( );}

DETAILED DESCRIPTION OF EXECUTION OBJECT B OF CLASS BANK IS CREATED 2345 IS PASSED TO ACC_NUM & 500 TO BAL NOW ACC_NO=ACC_NUM & BALANCE=BAL NOW OBJECT B OF CLASS BANK CALLS DISPLAY( )

COPY CONSTRUCTOR COPY CONSTRUCTOR IS USED TO ASSIGN ONE OBJECT WITH THE VALUES OF ANOTHER OBJECT OF THE SAME CLASS.. HERE ADDRESS OF THE DATA MEMBERS OR PARAMETERS ARE PASSED TO THE OBJECT….. A DETAILED ILLUSTRATION OF THE COPY CONSTRUCTOR IS SHOWN IN THE NEXT PAGE…. A DETAILED ILLUSTRATION OF THE COPY CONSTRUCTOR IS SHOWN IN THE NEXT PAGE….

ILLUSTRATION OF COPY CONSTRUCTOR THE FOLLWING CODE SPECIFIES IT:

COPY CONSTRUCTOR AN ANMBIGUOUS ELEMENT: the main purpose of copy const. to assign values of one object to another object can be achieved even In the simpler form as: the main purpose of copy const. to assign values of one object to another object can be achieved even In the simpler form as: class play { int price; int price; char item[20]; char item[20]; public: public: void read( ); void read( ); void show( ); void show( );}; Play obj1,obj2; Obj2=obj1; // this method of assigning objects is allowed

Have a look dear !! class play { int price; int price; char item[20]; char item[20]; public: public: void read( ); void read( ); void show( ); void show( );}; Void play::read( ) { ; ; ; ;} Void play::show( ) { ; ;} Void main() { play obj1,obj2; play obj1,obj2; obj2=obj1; obj2=obj1; obj1.read( ); //reading data thru object obj1 obj1.read( ); //reading data thru object obj1 obj2.show( ); // showing data thru object obj2 obj2.show( ); // showing data thru object obj2}

PRECAUTION

Hence we see that simple assignment of objects is a better option than copy constructors…. Hence we should use copy constructor only when pointers or address operator is used….

DESTRUCTOR

JUST AS THIS HAMMER HELPS US TO PULL OUT THE BOLT ONCE SCREWED IN,IN THE SAME WAY DESTRUCTOR REMOVES THE MEMORY ONCE ALLOCATED TO THE OBJECT BY THE CONSTRUCTOR.

DESTRUCTOR IS THE LAST FUNCTION TO BE CALLED BY THE COMPILER DURING EXECUTION OF ANY OBJECT ORIENTED PROGRAM.. AFTER EXECUTION OF A PROGRAM,THERE IS NO USE OF OBJECTS AND CLASSES OF THAT PROGRAM. HENCE MEMORY ALLOCATED TO THEM IS A MERE WASTAGE AND SHOULD BE RETURNED BACK TO THE SYSTEM.. DESTRUCTOR DOES THIS WORK..

THE DEFINITION OR DECLARATION OF A DESTRUCTOR IS SAME.. i.e. A DESTRUCTOR HAS THE NAME AS THAT OF THE CLASS… IN ADDITION TO IT,IT HAS A TILDE SIGN (~) BEFORE IT.. Class play { int price; int price; public: public: ~play( ) ~play( ) { cout<<“ destructor”; cout<<“ destructor”; } void read( ); void read( );};

LETS WIND UP HERE IS A COMPLETE PROGRAM WITH BOTH CONSTRUCTORS AND DESTRUCTORS…. #include<iostream.h>#include<conio.h> Class bank { int acc_no; int acc_no; int bal; int bal; public: public: bank( ) // CONSTRUCTOR bank( ) // CONSTRUCTOR { cout<<“ constructor is called \n”; cout<<“ constructor is called \n”; }

~bank( ) ~bank( ) { cout<<“ DESTRUCTOR IS CALLED \n”; cout<<“ DESTRUCTOR IS CALLED \n”; } void read_data( ) void read_data( ) { cout<<“ ENTER ACCOUNT NO. & BALANCE \n”; cout<<“ ENTER ACCOUNT NO. & BALANCE \n”; cin>>acc_no>>bal; cin>>acc_no>>bal; } void display( ); void display( );};

void bank::display( ) { cout<<“bank account no is”<<acc_no<<endl; cout<<“bank account no is”<<acc_no<<endl; cout<<“balance is “<<bal; cout<<“balance is “<<bal;} void main( ) { bank obj; bank obj; obj.read_data( ); obj.read_data( ); obj.display( ); obj.display( ); getch( ); getch( );}

EXECUTION & OUTPUT OUTPUT WINDOW IS AS:> ENTER ACCOUNT NO & BALANCE _ OUTPUT IS: CONSTRUCTOR IS CALLED CONSTRUCTOR IS CALLED BANK ACCOUNT IS BANK ACCOUNT IS BALANCE IS 500 BALANCE IS 500 DESTRUCTOR IS CALLED DESTRUCTOR IS CALLED