Object Oriented Programming(OOP).

Similar documents
Introduction Of Classes ( OOPS )

Data Structures using OOP C++ Lecture 3

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Lecture 18 Tao Wang 1

Chapter 10 Introduction to Classes

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe


Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Some important concept in oops are 1) Classes 2) Objects 3) Data abstraction & Encapsulation. 4) Inheritance 5) Dynamic binding. 6) Message passing

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Recharge (int, int, int); //constructor declared void disply();

More C++ : Vectors, Classes, Inheritance, Templates

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

C++ 8. Constructors and Destructors


Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Object Oriented Design

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Topic 10: Introduction to OO analysis and design

Object Oriented Programming. Solved MCQs - Part 2

System Design and Programming II

Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

VALLIAMMAI ENGINEERING COLLEGE

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Padasalai.Net s Model Question Paper

CS304 Object Oriented Programming Final Term

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

cout<< \n Enter values for a and b... ; cin>>a>>b;

END TERM EXAMINATION

AN OVERVIEW OF C++ 1

INHERITANCE: EXTENDING CLASSES

Interview Questions of C++

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Unit IV Contents. ECS-039 Object Oriented Systems & C++

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Inheritance, and Polymorphism.

OBJECT ORIENTED AND MULTICORE PROGRAMMING LABORATORY

CLASSES AND OBJECTS IN JAVA

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Programming 2. Object Oriented Programming. Daniel POP

Abstraction in Software Development

And Even More and More C++ Fundamentals of Computer Science

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Fundamentals of Programming Session 24

Constructor - example

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Introduction to Classes

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Cpt S 122 Data Structures. Introduction to C++ Part II

Get Unique study materials from

10. Abstract Data Types


Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Classes: A Deeper Look

JAVA: A Primer. By: Amrita Rajagopal

MAN4A OBJECT ORIENTED PROGRAMMING WITH C++ Unit I - V

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

6.096 Introduction to C++ January (IAP) 2009

CS304 Final Term Examination Spring 2006 Time Allowed: 150 Minutes. + operator for the complexclass?

Constructors for classes

Object Oriented Modeling

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

OBJECT ORİENTATİON ENCAPSULATİON

Object Oriented Programming with c++ Question Bank

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

C++ (classes) Hwansoo Han

Object Oriented Programming COP3330 / CGS5409

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Fundamental Concepts and Definitions

Object-Oriented Design (OOD) and C++

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

Object Orientated Analysis and Design. Benjamin Kenwright

CS201- Introduction to Programming Current Quizzes

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

2. Introducing Classes

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Object Oriented Pragramming (22316)

Object-Oriented Programming (OOP) Fundamental Principles of OOP

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

C++ Important Questions with Answers

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

Fast Introduction to Object Oriented Programming and C++

Transcription:

Object Oriented Programming(OOP). OOP terminology: Class : A class is a way to bind data and its associated function together. It allows the data to be hidden. class Crectangle Data members length; breadth; Member Functions set_val(); area(); Perimeter (); print_val(); Data members can be viewed as the attributes or the features of the class and the member functions are the actions to be performed on the data. This concept of integrating data and functions together is termed as Data encapsulation. In the above example length and the breadth are the attributes or the characteristics of a rectange i.e. using these parameters we can describe a rectangle. The 4 functions declared above are the actions to be performed on these data. For e.g the function area acts on the attributes length and breadth and printouts out the are of the rectangle. Base and height are the attributes of a triangle. Class Specification comprises of the following two parts 1) Class declaration. 2) Class function definition.

Class declaration class class-name private: variable declaration; function declaration; public: variable declaration; function declaration; ; This semicolon is important and is used to terminate the class declaration The data members and the member functions can be declared in the public section and the private section. These two are collectively referred to as members. Private section: This section is used to conceal the data (data hiding ) and the members declared in this section are blocked from external access, i.e the functions outside the class declaration will be prevented from accessing these members, with one exception i.e friend function. These members can only be accessed by the member functions. Public section: This section is transparent to all the functions outside the class declaration. *By default all the members of the class are private, i.e if the visibility tag is not specified explicitly then the members will be considered as private. 2)Defining member functions There are two ways to do that ¾ Outside the class declaration: The prototype is written in the class declaration, then by using the scope resolution operator the function is defined outside the class declaration. ¾ Inside the class declaration(inline functions): These functions are defined inside the class declaration.

:: -- scope resolution operator, enables us to define a function outside the class declaration. class CRectangle int length,breadth; public: void set_values (int, int); //function inside the class definition (inline function) int area (void) return (length*breadth); int perimeter(void); ; void CRectangle::set_values (int a, int b) length = a; breadth = b; int CRectangle::perimeter(void) return(2*(length + breadth)); Creation of Object: Objects are the instances of the class and possess all the features of the class Syntax classname objectname; e.g. CRectangle rect1; CRectangle rect1,rect2, rect3, rect4; //multiple objects This declaration is similar to the following declaration int a, b; // a and b are the variables of the type integer. rect1, rect2, rect3 are the instances of the class CRectangle and possess all the characteristics of the class CRectangle including the access to its member functions.

rect1, rect2, rect3 are rectangles which share the same attributes length and breadth and can be acted upon by the same set of actions(functions). Accessing class members: Syntax Object-name.member; For e.g. rect1.area(); rect2.set_val(); Accessing the private members using the above procedure is considered invalid e.g. rect1.length = 10; rect1.breadth = 6; Invalid expressions Private members can only be accessed by using the member functions. e.g. rect1.area(); rect1.perimeter(); To understand, class definition, object creation, accessing class members and defining the member function refer to the inclass example [33] on the class website.. Accessing the private functions The functions which are defined in the private section cannot be accessed by any function outside the class. For accessing such functions we make use of the member functions. refer to example[34]. Arrays with in a class : The usage of arrays inside the class is identical to their usage inside a C++ program. e.g. class array int a[10]; dot operator public: void set_val(void); ; Function prototype

void array:: set_val(void) for(int i = 0;i<=10 ; i++) a[i] = i; int main() array array1;// creating an object array1 of the class array array1.set_val(); return 0; Array of Objects e.g. class employee char name[30]; float age; public: void getdata(void); void putdata(void); ; int main() employee worker[75];//declares an array of objects of the class employee for(int i = 0;i<75;i++) worker[i].putdata(); // accessing member functions using the object. return 0; *the above program is incomplete, the functions putdata and getdata aren t defined

Passing Objects as Function Arguments : This concept is similar to the concept of passing variables, arrays to the functions. There are two kinds of function calls ¾ Call by reference. ¾ Call by value. Call by value: A copy of the object is passed to the function, but any change made to the object inside the function is not reflected on the object used to call the function. i.e in order to have the effect felt outside we need to specify an explicit return operation e.g return(rect1); //returns an object where rect1 is an object to of the class CRectangle. To understand this concept better, refer to inclass example[35]. Constructors and Destructors: Constructors In all the examples considered so far, the member function set_val() has been used to initialize the private members of the object. CRectangle rect1; rect1.set_val(10,6); //assigns the values 10 and 6 to the length and the breadth of the //rectangle. The above two steps of creation of an object and the initialization of the data members can be clubbed together by using a special member function called the constructor which helps an object to initialize itself when it is created. Whenever an object is created a constructor is invoked.unless we intend to perform some operation like initializing the variables at the time of creation, we need not explicitly define a constructor. There are two kinds of Constructors ¾ Default Constructors. ¾ Parameterized Constructors.

Characteristics of Constructors ¾ They bear the same name as the class, this is how the C++ compiler recognizes that the function defined is a constructor. ¾ They should be declared in the public section of the class declaration. ¾ They do not have return types and hence cannot return values. ¾ We can define a number of constructors by the same in the class definition,this concept is called as Constructor Overloading. C++ compiler distinguishes between these constructors on the basis of their arguments ie default or parameterized constructor. Destructors: Destructor as the name implies is used to destroy objects that have been created by the constructor. It is used to release the memory which was allocated when the object was created. It is invoked automatically by the compiler upon exit from the program. It is written in the following manner ~CRectangle() refer to the inclass example [37]. Friend Functions: Recall that private members cannot be accessed from outside the class i.e. non-member functions cannot access the private members. There can arise a situation, which might require two classes to share a particular function. In such situations, C++ allows common functions to be made friendly with both classes, thereby allowing the function to have an access to the private data of the classes. Features of the Friend Classes ¾ Friend Functions need not be a member of any of the classes. ¾ It is not in the scope of the class to which it has been declared. ¾ Since it is not in the scope of the class it is declared, it need not be called using the object of that class. It can be invoked like a normal function with out the object reference. ¾ Unlike the member functions it cannot access the member name directly and has to use the object name and the dot operator with each member name.

¾ It can be declared in the public or private section of a class without changing its meaning. ¾ It has objects as arguments. To understand this concept better refer to the inclass example [35] on the class website. References: [1] C++ for Engineers and Scientists, Gary J. Bronson. [2] C++ How To Program, Deitel and Deitel. [3] Object Oriented Programming in Turbo C++, Robert Lafore. [4] Object-Oriented Programming, E Balagurusamy.