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

Similar documents
Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

C++ Classes, Constructor & Object Oriented Programming

Object Oriented Pragramming (22316)

STRUCTURING OF PROGRAM

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

UNIT- 3 Introduction to C++

Constructor - example

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

Downloaded from

PROGRAMMING IN C++ COURSE CONTENT

Object Oriented Programming. Solved MCQs - Part 2

C++ Important Questions with Answers


Interview Questions of C++

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

UNIT-2 Introduction to C++

Short Notes of CS201

Module Operator Overloading and Type Conversion. Table of Contents

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

CS201 - Introduction to Programming Glossary By

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

What are the characteristics of Object Oriented programming language?

CS304 Object Oriented Programming

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

Get Unique study materials from

Chapter 1. Principles of Object Oriented Programming

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

AN OVERVIEW OF C++ 1

CS304 Object Oriented Programming Final Term

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

UNIT POLYMORPHISM

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Operators. Java operators are classified into three categories:

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

VALLIAMMAI ENGINEERING COLLEGE

PESIT Bangalore South Campus

CS201 Some Important Definitions

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

Sri Vidya College of Engineering & Technology

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

An Object Oriented Programming with C

Introduction to Programming Using Java (98-388)

CS201 Latest Solved MCQs

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

Instantiation of Template class

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

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

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Absolute C++ Walter Savitch

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Data Structures using OOP C++ Lecture 3

Inheritance, and Polymorphism.

Object-Oriented Programming

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

CS 162, Lecture 25: Exam II Review. 30 May 2018

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

Fundamental of Programming (C)

DEPARTMENT OF MATHS, MJ COLLEGE

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming

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

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

Variables. Data Types.

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Object-Oriented Design (OOD) and C++

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Index COPYRIGHTED MATERIAL

Inheritance. Transitivity

Programming, numerics and optimization

Object Oriented Programming

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

OOP THROUGH C++(R16) int *x; float *f; char *c;

C++_ MARKS 40 MIN

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Programming. C++ Basics

6.096 Introduction to C++ January (IAP) 2009

Tokens, Expressions and Control Structures

Polymorphism. Zimmer CSCI 330

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

- HALF YEARLY EXAM ANSWER KEY DEC-2016 COMPUTER SCIENCE ENGLISH MEDIUM

DE70/DC56 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2014

Transcription:

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

OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2

WHAT IS A MODEL? A model is an abstraction of something Purpose is to understand the product before developing it Examples Model Highway maps Architectural models Mechanical models

EXAMPLE OO MODEL

EXAMPLE OO MODEL Objects Ali House Car Tree Interactions Ali lives in the house Ali drives the car Ali lives-in House drives Car Tree

OBJECT ORIENTED PROGRAMMING Objects have both data and methods Objects of the same class have the same data elements and methods Objects send and receive messages to invoke actions 6

BASIC TERMINOLOGY object - An object is an instance of a class which combines both data and functions together. Method - an action performed by an object (a verb) Attribute - description of objects in a class class - a category of similar objects - does not hold any values of the object s attributes By default all members declared inside a class are private to that class 7

STRUCTURE OF C++ PROGRAM HEADERS CLASS DECLARATION MEMBER FUNCTION DEFINITIONS MAIN FUNCTION 8

FEATURES OF OOP 1. Classes 2. Objects 3. Data encapsulation 4. Data abstraction 5. Inheritance 6. Polymorphism 7. Dynamic binding 8. Message passing 9

BENEFITS OF OOP OOP offers better implementation OOP offers better data security OOP offers better code reusability OOP offers more flexibility User defined data types can be easily constructed 10

CLASSES IN C++ A class definition begins with the keyword class. The body of the class is contained within a set of braces, (notice the semi-colon). class class_name... }; }; Any valid identifier Class body (data member + methods) 11

CLASS EXAMPLE This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle private: double radius; public: void setradius(double r); double getdiameter(); double getarea(); double getcircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius) 12

class Circle private: double radius; public: Circle() radius = 0.0;} Circle(int r); void setradius(double r)radius = r;} double getdiameter() return radius *2;} double getarea(); double getcircumference(); }; Circle::Circle(int r) radius = r; } double Circle::getArea() return radius * radius * (22.0/7); } double Circle:: getcircumference() return 2 * radius * (22.0/7); } Scope resolution operator :: Defined outside class 13

FUNDAMENTAL OF OOP C++ #include directive main () function Class Variables & Functions Input, output operators Cascading I/O operators Comments Example C++ program Creating source file Compile & Execute the c++ programs 14

INPUT OPERATORS cin >> variable-name; Meaning: read the value of the variable called <variable-name> from the user Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character; 15

OUTPUT OPERATORS cout << variable-name; Meaning: print the value of variable <variable-name> to the user cout << any message ; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << This is my character: << my-character << end << endl; 16

DIFFERENCE B/W C AND C++ C Language C++ Language Procedural Programming Language Object Oriented Programming Language Headerfile : #include<stdio.h> #include<iostream.h> \n is used to go to the next line we can use endl statement Local variables declared only the start of a C program It can be declared anywhere in a program, before they are used Return type for a function is optional Return type must be specified Do not permit data Hiding They permit data hiding Bydefault structure members are public class members are private We can call a main() function within a program This is not allowed 17

TOKENS, KEYWORDS AND IDENTIFIERS Tokens, a smallest individual units. Keywords, identifiers, constants,..etc Keywords implements specific c++ language features Identifiers refers the names of variables, functions, arrays..etc 18

DATA TYPES Datatypes User defined: Class Structure Union Enum Built-in Derived: Array Pointers Function 19

STRUCTURE Structure is the collection of variables of different types under a single name for better visualization of problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can hold data of one or more types. struct person char name[50]; int age; float salary; }; How to define a structure variable? person bill; How to access members of a structure? bill.age = 50; 20

UNION Unions in C++ is a user defined data type that uses the same memory as other objects from a list of objects. At an instance it contains only a single object. Example #include <iostream.h> union Emp int num; double sal; }; int main() Emp value; value.num = 2; cout << "Employee Number::" << value.num << "\nsalary is:: " << value.sal << endl; value.sal = 2000.0; cout << "Employee Number::" << value.num << "\nsalary is:: " << value.sal << endl; return 0; } 21

BUILT-IN DATATYPES 22

ARRAY Array is a collection of elements of same type referred using a unique name. Each element of an array are stored in memory locations indexed from 0 through n number of its elements. The lowest indexed will be the first element and highest indexed the last element of an array. type array_name[array_size_1] int age[5]; 23

EXAMPLE #include <iostream.h> void main() int i; float mark[6]; cout << "Enter the marks of your 6 subjects:: \n"; for(i=0; i<6; i++) cin >> mark[i]; } float sum=0; for(i=0;i<6;i++) sum += mark[i]; } float ave = sum/6; cout << "Average Marks is::" << ave << '\n'; } 24

POINTERS Store the address of other variable int x; // int variable int *ip; // int pointer ip=&x; //address of x assigned to ip *ip=10; // 10 assigned to x indirectly 25

OPERATORS Scope resolution operator :: Member dereferencing operator ->,*,::* Memory management operator malloc(),sizeof(),new,delete free () Manipulators endl and setw Type cast operators type_name (expression) 26

OPERATORS Arithmetic operators operators + met Addition Example x=10,y=5 x+y -> 21 - Subtraction X-y -> 5 * Multiplication X*y -> 50 / Division x/y -> 2 % Modulo Division X%y -> 0 Relational Operators Opera tor Meaning Example Result valu e < Less Than x=10,y=5 X<y False 0 > Greater Than X>y True 1 <= Less than or Equal to X<=y False 0 >= Greater than or equal to X>=y True 1!= Not Equal to X!=y False 0 == Equal To X==y False 0 27

EXPRESSIONS Constant Expressions Integral expressions Float expressions Pointer expressions Relational expressions Logical expressions Bitwise expressions 28

CONTROL STRUCTURES if if else Nested if switch do while while for 29

LOOPS For loop for(initialization statement; test expression; update statement) codes to be executed; } While loop while (test expression) statement/s to be executed. } 30

LOOPS Do while do statement/s; } while (test expression); 31

SWITCH switch (n) case constant1: code/s to be executed if n equals to constant1; break; case constant2: code/s to be executed if n equals to constant2; break;... default: code/s to be executed if n doesn't match to any cases; } 32

POINTERS Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

POINTERS A pointer is a variable used to store the address of a memory cell. We can use the pointer to reference this memory cell datatype *var-name; Memory address: 1020 integer 1024 100 1032 1024 pointer 34

OPERATIONS 1. we define a pointer variables 2. assign the address of a variable to a pointer and 3. finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand 35

POINTER VARIABLE Declaration of Pointer variables type* pointer_name; //or type *pointer_name; where type is the type of data pointed to (e.g. int, char, double) Examples: int *n; RationalNumber *r; 36

CONSTRUCTORS & DESTRUCTORS Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CONSTRUCTOR A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class It does not have any return type at all Constructors can be very useful for setting initial values for certain member variables 38

EXAMPLE class Line public: void setlength( double len ); double getlength( void ); Line(); // This is the constructor private: double length; Line::Line() int main( ) cout << "Object is being created ; Line line; } // set line length line.setlength(6.0); cout << line.getlength() <<endl; void Line::setLength( double len ) length = len; } }; double Line::getLength( void ) return length; } return 0; } 39

PARAMETERIZED CONSTRUCTOR: A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation 40

DESTRUCTOR A destructor is used to destroy the objects that have been created by the constructor class Line public: void setlength( double len ); double getlength( void ); Line(); // This is the constructor `Line(); // This is the destructor private: double length; }; 41

OPERATOR OVERLOADING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

UNARY OPERATOR OVERLOADING The unary operators operate on a single operand and following are the examples of Unary operators: The increment (++) and decrement (--) operators. The unary minus (-) operator. The logical not (!) operator. 43

EXAMPLE 1 class complex int a,b,c; public: void getvalue() cout<<"enter the Two Numbers:"; cin>>a>>b; } void operator++() a=++a; b=++b; } void operator--() a=--a; b=--b; } void display() cout<<a<<"+\t"<<b<<"i"<<endl; } }; void main() clrscr(); complex obj; obj.getvalue(); obj++; cout<<"increment Complex Number\n"; obj.display(); obj--; cout<<"decrement Complex Number\n"; obj.display(); getch(); } 44

IN C++, FOLLOWING OPERATORS CAN NOT BE OVERLOADED:. (Member Access or Dot operator)?: (Ternary or Conditional Operator ) :: (Scope Resolution Operator).* (Pointer-to-member Operator ) sizeof (Object size Operator) 45

BINARY OPERATOR OVERLOADING This feature in C++ programming that allows programmer to redefine the meaning of operator when they operate on class objects is known as operator overloading. Complex number (real + imag i) addition 5+6i + 7+3i 46

INHERITANCE Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

INHERITANCE Inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. Reuse the code functionality and fast implementation time. NOTE : All members of a class except Private, are inherited

SYNTAX class Subclass_name : access_mode Superclass_name Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.

EXAMPLE class Animal // base class public: int legs = 4; }; class Dog : public Animal // derived class public: int tail = 1; }; int main() Dog d; cout << d.legs; cout << d.tail; }

A DERIVED CLASS INHERITS ALL BASE CLASS METHODS WITH THE FOLLOWING EXCEPTIONS: Constructors, destructors and copy constructors of the base class. Overloaded operators of the base class. The friend functions of the base class

TYPES OF INHERITANCE 1. Single Inheritance 2. Multiple Inheritance 3. Hierarchical Inheritance 4. Multilevel Inheritance 5. Hybrid Inheritance

SINGLE INHERITANCE In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance.

MULTIPLE INHERITANCE In this type of inheritance a single derived class may inherit from two or more than two base classes.

MULTILEVEL INHERITANCE In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other.

HIERARCHICAL INHERITANCE In this type of inheritance, multiple derived classes inherits from a single base class.

HYBRID INHERITANCE Hybrid Inheritance is Multilevel Inheritance. combination of Hierarchical and

VIRTUAL BASE CLASS Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited

EXAMPLE class A... }; class B1 : virtual public A... }; class B2 : virtual public A... }; class C : public B1, public B2...// only one copy of A...// will be inherited };

VIRTUAL FUNCTIONS Virtual Function is a function in base class, which is overrided in the derived class Virtual Keyword is used to make a member function of the base class Virtual. PURE VIRTUAL FUNCTION If expression =0 is added to a virtual function then, that function is becomes pure virtual function. Note that, adding =0 to virtual function does not assign value, it simply indicates the virtual function is a pure function. If a base class contains at least one virtual function then, that class is known as abstract class. 60

EXCEPTION HANDLING An exception is a problem that arises during the execution of a program. Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw. 61

TRY, CATCH & THROW throw: A program throws an exception when a problem shows up. This is done using a throw keyword. catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks. 62

TRY & CATCH try // protected code } catch( ExceptionName e1 ) // catch block } catch( ExceptionName e2 ) // catch block } catch( ExceptionName en ) // catch block } You can list down multiple catch statements to catch different type of exceptions in case your try block raises more than one exception in different situations. 63

THROWING EXCEPTIONS Exceptions can be thrown anywhere within a code block using throw statements. The operand of the throw statements determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown. 64

EXAMPLE double division(int a, int b) if( b == 0 ) // type of exception throw "Division by zero condition!"; } return (a/b); } 65

CATCHING EXCEPTIONS The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch. 66

DEFINE NEW EXCEPTIONS You can define your own exceptions by inheriting and overriding exception class functionality. Allows you can use std::exception class to implement your own exception in standard way 67