Download presentation
Presentation is loading. Please wait.
Published byMaximilian Fisher Modified over 9 years ago
2
MY COLLEGE: BITM SANTINIKETAN
4
Object oriented programming lets check through C++
5
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….
6
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…
7
EXAMPLES OF OBJECTS:
8
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….
9
EXAMPLE OF CLASS FORMATION
10
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..
11
THIS PROPERTY OF COMBINING DATAS AND FUNCTIONS INTO A SEPARATE ENTITY CALLED CLASS IS KNOWN AS ENCAPSULATION..
12
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
13
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;
14
MORE METHODS OF OBJECT DECLARATION METHOD 1: Class employee { --------; --------; }obj; METHOD 2: Class employee { --------; --------; }; Employee obj;
15
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…
16
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…….
17
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…
18
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: :: ::
19
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..
20
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; }};
21
> > AFTERALL COMPLETION OF A TASK MATTERS.. SO THIS ONE IS FOR ALL OF U.. CLASS PLAY: ->NAME,PRICE INPUT( ); DISPLAY( ); ->OBJECT IS P
22
#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;}
23
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( );}
24
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.
25
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..
26
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( );};
27
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 );};
28
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 );};
29
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..
30
PASSING PARAMETERS
31
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( )
32
#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( );}
33
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( )
34
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….
35
ILLUSTRATION OF COPY CONSTRUCTOR THE FOLLWING CODE SPECIFIES IT:
36
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
37
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}
38
PRECAUTION
39
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….
40
DESTRUCTOR
41
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.
42
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..
43
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( );};
44
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”; }
45
~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( );};
46
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( );}
47
EXECUTION & OUTPUT OUTPUT WINDOW IS AS:> ENTER ACCOUNT NO & BALANCE _ 302134 302134 500 500 OUTPUT IS: CONSTRUCTOR IS CALLED CONSTRUCTOR IS CALLED BANK ACCOUNT IS 302134 BANK ACCOUNT IS 302134 BALANCE IS 500 BALANCE IS 500 DESTRUCTOR IS CALLED DESTRUCTOR IS CALLED
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.