Docsity

OBJECT ORIENTED PROGRAMMING DIGITAL NOTES, Summaries of Object Oriented Programming

Toggle sidebar
bg1
OBJECT ORIENTED PROGRAMMING
DIGITAL NOTES
B.TECH
(I YEAR – II SEM)
(2018-19)
DEPARTMENT OF CSE & IT
MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY
(Autonomous Institution – UGC, Govt. of India)
Recognized under 2(f) and 12 (B) of UGC ACT 1956 (Affiliated to JNTUH, Hyderabad, Approved by AICTE -
Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified) Maisammaguda, Dhulapally (Post Via.
Hakimpet), Secunderabad – 500100, Telangana State, India.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d

Partial preview of the text

Download OBJECT ORIENTED PROGRAMMING DIGITAL NOTES and more Summaries Object Oriented Programming in PDF only on Docsity!

OBJECT ORIENTED PROGRAMMING

DIGITAL NOTES

B.TECH

(I YEAR – II SEM)

DEPARTMENT OF CSE & IT

MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY

(Autonomous Institution – UGC, Govt. of India)

Recognized under 2(f) and 12 (B) of UGC ACT 1956 (Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified) Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India.

I Year B.Tech II SEM L T/P/D C 4 1/-/- 3

(R18A0502)OBJECT ORIENTED PROGRAMMING

Objectives

To teach the student the concepts of object oriented and procedure programming To differentiate between functions, classes and objects To learn to overload functions and operators To design applications using dynamic memory management techniques To teach the student to implement generic programming and exception handling

Unit I

Introduction to Object Oriented Programming: Object oriented paradigm-Differences between Object Oriented Programming and Procedure oriented programming, Basic concepts of Object

Oriented Programming, Encapsulation, Inheritance and Polymorphism, Benefits of OOP, Structure of

a C++ program, namespace, Data types, C++ tokens, Identifiers, Variables, Constants, Operators, Control structures & Loops.

Unit-II Functions, Classes and Objects:

Introduction of Classes, Class Definition, Defining a Members,Objects,Access Control, Class Scope,

Scope Resolution Operator, Inline functions, Memory Allocation for Objects, Static Data Members, Static Member Functions, Arrays of Objects, Objects as Function Arguments, Friend Functions.

Unit-III Constructors, Destructors, Inheritance:

Introduction to Constructors, Default Constructors, Parameterized Constructors, Copy Constructors, Multiple Constructors in a Class, Destructors.

Inheritance :

Introduction to inheritance, Defining Derived Classes, Single Inheritance, Multiple Inheritance, Multi level Inheritance, Hierarchical Inheritance, Hybrid Inheritance.

Unit-IV Pointers, Virtual Functions and Polymorphism:

Introduction to Memory management, new operator and delete operator, Pointers to objects,

Pointers to Derived Classes, Polymorphism, Compile time polymorphism, Run time polymorphism, Virtual Functions, Overloading- Function Overloading, Operator overloading.

Unit-V Templates and Exception handling:

Introduction to Templates, Class Templates, Class Templates with Multiple Parameters, Function

Templates, Function Templates with Multiple Parameters.

UNIT -

Concepts of Object Oriented programming: Object oriented paradigm-differences between Object Oriented Programming and Procedure oriented programming, Basic concepts of Object Oriented Programming,Encapsulation, Inheritance and Polymorphism. Benefits of OOP .Structure of a C++ program, namespace, Data types, C++ tokens, identifiers, variables, constants, operators, control structures & loops.

Overview of C language:

1.C language is known as structure oriented language or procedure oriented language 2.Employs top-down programming approach where a problem is viewed as a sequence of tasks to be performed. 3.All program code of c can be executed in C++ but converse many not be possible

  1. Function overloading and operator overloading are not possible.
  2. Local variables can be declared only at the beginning of the block.
  3. Program controls are through jumps and calls to subroutines. 7.Polymorphism, encapsulation and inheritance are not possible. For solving the problems, the problem is divided into a number of modules. Each module is a subprogram.
  4. Data abstraction property is not supported by procedure oriented language.
  5. Data in procedure oriented language is open and can be accessed by any function.

Overview of C++ language:

  1. C++ can be considered as an incremental version of c language which consists all programming language constructs with newly added features of object oriented programming. 2.c++ is structure(procedure) oriented and object oriented programming language. 3.The file extension of C++ program is “.CPP”
  2. Function overloading and operator overloading are possible.
  3. Variables can be declared in inline i.e when required
  4. In c++ more emphasis is give on data rather than procedures 7.Polymorphism, encapsulation and inheritance are possible.
  5. Data abstraction property is supported by c++.
  6. Data access is limited. It can be accessed by providing various visibility modes both for data and member functions. there by providing data security by data hiding 10.Dymanic binding is supported by C++ 11..It supports all features of c language 12.It can be called as an incremental version of c language

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)

Procedure Oriented Programming program is divided into small parts called functions. Importance is not given to data but to functions as well as sequence of actions to be done.

follows Top Down approach.

It does not have any access specifier.

Data can move freely from function to function in the system.

Object Oriented Programming

program is divided into parts called objects.

Importance is given to the data rather than procedures or functions because it works as a real world.

OOP follows Bottom Up approach. OOP has access specifiers named Public, Private, Protected, etc. objects can move and communicate with each other through member functions.

To add new data and function in POP is not OOP provides an easy way to add new data so easy. and function. Most function uses Global data for sharing In OOP, data can not move easily from 7 that can be accessed freely from function to function to function,it can be kept public or function in the system. private so we can control the access of data.

8

It does not have any proper way for hiding OOP provides Data Hiding so provides more data so it is less secure. security. In OOP, overloading is possible in the form of 9 Overloading is not possible. Function Overloading and Operator Overloading. Example of Procedure Oriented

10 Programming are : C, VB, FORTRAN, Example of Object Oriented Programming are : C++, JAVA, VB.NET, C#.NET. Pascal.

Principles( or features) of object oriented programming:

  1. Encapsulation
  2. Data abstraction
  3. Polymorphism
  4. Inheritance
  5. Dynamic binding
  6. Message passing Encapsulation: Wrapping of data and functions together as a single unit is known as encapsulation. By default data is not accessible to outside world and they are only accessible through the functions which are wrapped in a class. prevention of data direct access by the program is called data hiding or information hiding

Data abstraction : Abstraction refers to the act of representing essential features without including the back ground details or explanation. Classes use the concept of abstraction and are defined as a list of attributes such as size, weight, cost and functions to operate on these attributes. They encapsulate all essential properties of the object that are to be created. The attributes are called as data members as they hold data and the functions which operate on these data are called as member functions. Class use the concept of data abstraction so they are called abstract data type (ADT)

Polymorphism: Polymorphism comes from the Greek words “poly” and “morphism”. “poly” means many and “morphism” means form i.e.. many forms. Polymorphism means the ability to take more than one form. For example, an operation have different behavior in different instances. The behavior depends upon the type of the data used in the operation. Different ways to achieving polymorphism in C++ program:

  1. Function overloading 2) Operator overloading #include using namespace std; int main() {int a=4; a=a<<2;

cout<<”a=”<<a<<endl; return 0; }

Inheritance: Inheritance is the process by which one object can acquire the properties of another.

1. DOCUMENTATION SECTION : comes first and is used to document the use of logic or reasons in your program. It can be used to write the program's objective, developer and logic details. The documentation is done in C language with /* and */. Whatever is written between these two are called comments. 2. LINKING SECTION : This section tells the compiler to link the certain occurrences of keywords or functions in your program to the header files specified in this section. e.g. #include using namespace std;  (^) directive causes the preprocessor to add the contents of the iostream file to the program. It contains declarations for cout and cin.  cout is a predefined object that represents the standard output stream. The operator << is an insertion operator, causes the string in double quotes to be displayed on the screen.

screen

cout << “C++” Object Insertion Operator variable

The statement cin>>n; is an input statement and causes the program to wait for the user to type in a number. The number keyed is placed on the variable “n”. The identifier cin is a predefined object in C++ that corresponds to the standard input stream. The operator >> is known as extraction operator. It extracts the value from the keyboard and assigns it to the value variable on its right.

Object Extraction operator variable

cin (^) >> 45.

Keyboard

3. DEFINITION SECTION : It is used to declare some constants and assign them some value. e.g. #define MAX 25 Here #define is a compiler directive which tells the compiler whenever MAX is found in the program replace it with 25. 4. GLOBAL DECLARATION SECTION : Here the variables and class definations which are used through out the program (including main and other functions) are declared so as to make them global(i.e accessible to all parts of program). A CLASS is a collection of data and functions that act or manipulate the data. The data components of a class are called data members and function components of a class are called member functions A class ca also termed as a blue print or prototype that defines the variable or functions common to all objects of certain kind. It is a user defined data type

e.g.

int i; //this declaration is done outside and before main()

5. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or the functions which our program needs.

void display() { cout<<”C++ is better that C”; } SIMPLE „C++ ‟ PROGRAM: #include using namespace std; void display() { cout<<”C++ is better that C”; } int main() { display() return 0; }

6. MAIN FUNCTION SECTION : It tells the compiler where to start the execution from main() { point from execution starts } main function has two sections

  1. declaration section : In this the variables and their data types are declared.
  2. Executable section or instruction section : This has the part of program which actually performs the task we need.

namespace:

namespace is used to define a scope that could hold global identifiers. ex:-namespace scope for c++ standard library. A classes ,functions and templates are declared within the namespace named std using namespace std;-->directive can be used.

user defined name space: syntax for defining name space is

namespace namespace_name { //declarations of variables.functions,classes etc... } ex: #include using namespace std; namespace sample {` int m; void display(int n)

integer data type: - This data type is used to store whole numbers. These numbers do not contain the decimal part. The size of the integer depends upon the world length of a machine (16-bit or 32-bit). On a 16-bit machine, the range of integer values is - 32,768 to +32,767.integer variables are declared by keyword int. C provides control over range of integer values and storage space occupied by these values through the data types: short int, int, long int in both signed and unsigned forms.

Signed integers: (16-bit machine): A signed integer uses 1 bit for sign and 15 bits for the magnitude of the number

MSB(most significant bit)

(^100) (10)

= (^00000000001100100) (2)

Representation of negative number :

-100(10)= (^1111111110011100) (2)

6 5 4 3 2 1 0 02 +02 +12 +12 +12 +02 +0*

= -32768+16384+8192+4096+2048+1024+512+256+128+0+0+26+8+4+0+0 =-

(^100) (10)

NOTE: Signed bit (MSB BIT) : 0 represents positive integer, 1 represents negative numbers

Unsigned integers : Unsigned integers use all 16 bits to store the magnitude. Stores numbers does not have any

sign & Size qualifier and range of integer data type on a 16-bit and machine are shown in the table:

MEMORY REQUIRED (^) RANGE OR STORAGE SIZE IN BYTES FORMAT DATA TYPE TURBO C GCC/ COMPILERS TURBO C GCC SPECIER ( 16 BIT) IN LINUX (32 BIT)^ ( 16 BIT) (32 BIT)

short int -^32768 -^32768 or 2 2 32767 To^32767 To^ %hd signed short int (-^2

15 to +

15

    1. (- 2

15 to +

15

short int 0 to 65535 0 to 65535 or 2 2 (0 to +2 - 1) (0 to +2 - 1) %hu signed short int signed int -^32768 - 2,147,843,648^ %d or 2 4 32767 To^ 2,147,843,647to^ or int (- 2

15 to +

15

    1. (- 2

31 to +

31

    1. %i unsigned int 0 to 65535 0 to 4,294,967, 2 4 (0 to +

16

    1. (0 to

32

  • 1 ) (^) %u long int - 2,147,843,648 - 2,147,843, or 4 4

to to signed long int 2,147,843,647 2,147,843,647 %ld (- 2

31 to +

31

    1. (- 2

31 to +

31

unsigned long int 0 to 4,294,967,295 0 to 4,294,967, 4 4 (0 to

32

  • 1 ) (0 to

32

  • 1 ) (^) %lu long long int (^) - 9223372036854775808 or signed long long (^) supportedNot^8 -------- 9223372036854775807 To^ %Ld

int (-^2

63 to +

63

Character data type : ( char )

A single character can be defined as a character data type. Character data type occupies one byte of memory for storage of character. The qualifiers signed or unsigned can be applied on char data type. char is the key word used for declaring variables size and range of character data type on 16 bit or 32 bit machine can be shown below

Data type MEMORY REQUIRED RANGE FORMAT SPECIER OR STORAGE SIZE (in bytes)

char or signed char (^1) - 128 to 127(- 2 7 to

2 - 1) (^) %c

Unsigned signed char 1 0 to 256 (0 to 2 - 1) %c

Floating Point Types:

Floating point number represents a real number with 6 digits precision occupies 4 bytes of memory. Floating point variables are declared by the keyword float. Double floating point data type occupies 8 bytes of memory giving 14 digits of precision. These are also known as double precision numbers. Variables are declared by keyword double long double refers to a floating point data type that is often more precise than double precision.

Rules for declaring Variables names: ^ ^ The first character must be an alphabet or underscore.  (^) It must consist of only letters, digits and underscore.  (^) Identifiers may have any length but only first 31 characters are significant.  (^) It must not contain white space or blank space.  (^) We should not use keywords as identifiers.

^ ^ Upper and lower case letters are different.  (^) Variable names must be unique in the given scope

 Ex:int a,b,a;//is in valid

Int a,b;//is valid

Variable declaration: The declaration of variable gives the name for memory location and its size and specifies the range of value that can be stored in that location. Syntax: Data type variable name; Ex: a 10 2000 int a=10; float x=2.3; x 2.300000 5000

KEYWORDS :

There are certain words, called keywords (reserved words) that have a predefined meaning in „C++‟ language. These keywords are only to be used for their intended purpose and not as identifiers. The following table shows the standard „C++‟ keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while class friend new delete this public private protected inline try throw catch template

CONSTANTS: Constants refer to values that do not change during the execution of a program. Constants can be divided into two major categories:

  1. Primary constants : a)Numeric constants  (^) Integer constants.  (^) Floating-point (real)

 constants. b)Character constants  (^) Single character constants  (^) String constants

  1. Secondary constants :  (^) Enumeration constants.

^ ^ Symbolic constants. ^ ^ Arrays, unions, etc.  Rules for declaring constants:

1.Commas and blank spaces are not permitted within the constant. 2.The constant can be preceded by minus (-) signed if required.

3.The value of a constant must be within its minimum bounds of its specified data type. Integer constants : An integer constant is an integer-valued number. It consists of sequence of digits. Integer constants can be written in three different number systems:

1.Decimal integer (base 10). 2.Octal integer (base 8). 3.Hexadecimal (base 16).

Decimal integer constant : It consists of set of digits, 0 to 9. Valid declaration: 0, 124, -56, + 67, 4567 etc. Invalid declaration: $245, 2.34, 34 345, 075. 23,345,00. it is also an invalid declaration. Note: Embedded spaces, commas, characters, special symbols are not allowed between digits

They can be preceded by an optional + or ± sign.

Octal integer : It consists of set of digits, 0 to 7.

Ex: 037, 0, 0765, 05557 etc. (valid representation) It is a sequence of digits preceded by 0. Ex: Invalid representations 0394: digit 9 is not permitted (digits 0 to 7 only) 235: does not begin with 0. (Leading number must be 0).

Hexadecimal integer: It consists of set of digits, 0 to 9 and alphabets A, B, C, D, E, and F. Hexadecimal integer is a sequence of digits preceded by 0x or 0X. We can also use a through f instead of A to F. Ex: 0X2, 0x9F, 0Xbcd, 0x0, 0x1. (Valid representations) Ex: Invalid representations: 0af, 0xb3g, 0Xgh. 0af: does not begin with 0x or 0X. 0xb3g, 0Xgh: illegal characters like g, h. (only a to f are allowed)

The magnitude (maximum value) of an integer constant can range from zero to some maximum value that varies from one computer to another. Typical maximum values for most personal computers are: (16-bit machines) Decimal integer constant: 32767 (215-1) Octal integer constant: 077777 Hexadecimal integer constant: 0X7FFF Note: The largest value that can be stored is machine dependent.

Floating point constants or Real constants : The numbers with fractional parts are called real constants.

These are the numbers with base-10 which contains either a decimal part or exponent (or both). Representation: These numbers can be represented in either decimal notation or exponent notation (scientific notation). Decimal notation: 1234.56, 75.098, 0.0002, -0.00674 (valid notations) Exponent or scientific notation: General form: Mantissa e exponent Mantissa: It is a real number expressed in decimal notation or an integer notation. Exponent: It is an integer number with an optional plus (+) or minus (-) sign. E or e : The letter separating the mantissa and decimal part. Ex: (Valid notations) 3 1.23456 E+ 3 (1.23456×10 ) 1 7.5098 e+ 1 (7.5098×10 )

2 E- 4 (2×10 ) These exponential notations are useful for representing numbers that are either very large or very small. Ex: 0.00000000987 is equivalent to 9.87e-

Character constants:- Single character constants : It is character(or any symbol or digit) enclosed within single quotes. Ex: „a‟ „1‟ „*‟ Every Character constants have integer values known as ASCII values

ASCII:- ASCII stands for American Standard Code for Information Interchange. Pronounced ask-ee, ASCII is a code for representing English characters as numbers, with each letter assigned a number from 0 to 255.Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an

/ division % modulo division(remainder) An arithmetic operation involving only real operands(or integer operands) is called real arithmetic(or integer arithmetic). If a combination of arithmetic and real is called mixed mode arithmetic. /C program on Integer Arithmetic Expressions/** #include<iostraem.h> void main() { int a, b; cout<"Enter any two integers"; cin>>a>>b; cout<<"a+b"<< a+b; cout<<"a-b"<< a-b; cout<<"ab"<< ab; cout<<"a/b"<< a/b; cout<<"a%b"<< a%b; }

OUTPUT: a+b= a-b= a*b= a/b= a% b=

2. RELATIONAL OPERATORS : We often compare two quantities and depending on their relation take certain decisions for that comparison we use relational operators. operator meaning

<

<=

= == !=

is less than is greater than is less than or equal to is greater than or equal to is equal to is not equal to / C program on relational operators/** #include<iostream.h> void main() { int a,b; clrscr(); cout<<"Enter a, b values:"; cin>>a>>b; cout<<"a>b"<< a>b; cout<<"a>=b"<< a>=b; cout<<"a<b"<< a<b; cout<<"a<=b"<< a<=b; cout<<"a==b"<< a==b; cout<<"a!=b"<< a!=b; } OUTPUT: Enter a, b values: 5 9 a>b: 0 //false a<b: 1 //true a>=a: 1 //true

a<=b: 1 //true a==b: 0 //false a!=b: 1 //true 3.LOGICAL OPERATORS: Logical Data: A piece of data is called logical if it conveys the idea of true or false. In C++ we use int data type to represent logical data. If the data value is zero, it is considered as false. If it is non -zero (1 or any integer other than 0) it is considered as true. C++ has three logical operators for combining logical values and creating new logical values:

Note:Below program works in compiler that support C99 standards #include<iostream.h> #include<stdbool.h> int main() { bool a,b; /logical and/ a=0;b=0; cout<<" a&&b "<< a&&b<<endl; a=0;b=1; cout<<" a&&b "<< a&&b<<endl; a=1;b=0; cout<<" a&&b "<< a&&b<<endl; a=1;b=1; cout<<" a&&b "<< a&&b<<endl; /logical or/ a=0;b=0; cout<<" a||b "<< a||b<<endl; a=0;b=1; cout<<" a||b "<< a||b<<endl; a=1;b=0; cout<<" a||b "<< a||b<<endl; a=1;b=1; cout<<" a||b "<< a||b<<endl; /logical not/ a=0; cout<<" a||b "<< a||b<<endl; a=1; cout<<" a||b "<< a||b<<endl; return 0; }

OUTPUT:

0&&0= 0&&1= 1&&0= 1&&1= 0||0= 0||1=

a=a+1; a--has the same effect as a=a-1. The difference between a++ and a+1 is, if ++ is after the operand, the increment takes place after the expression is evaluated. The operand in a postfix expression must be a variable. Ex1: int a=5; B=a++; Here the value of B is 5. the value of a is 6. Ex2: int x=4; y=x--; Here the value of y is 4, x value is 3 Prefix Increment/Decrement (++a/ --a) In prefix increment (decrement) the effect takes place before the expression that contains the operator is evaluated. It is the reverse of the postfix operation. ++a has the same effect as a=a+1. --a has the same effect as a=a-1. Ex: int b=4; A= ++b; In this case the value of b would be 5 and A would be 5. The effects of both postfix and prefix increment is the same: the variable is incremented by

  1. But they behave differently when they used in expressions as shown above. The execution of these operators is fast when compared to the equivalent assignment statement.

#include<iostream.h> int main() { int a=1; int b=5; ++a; cout<<"a="<<a<<endl; --b; cout<<"b="<<b<<endl; cout<<"a="<<a++<<endl; cout<<"a="<<a<<endl; cout<<"b="<<b--<<endl; cout<<"b="<<b<<endl; return 0; } a= b= a= a= b= b= 6.CONDITIONAL OPERATOR OR TERNARY OPERATOR:

A ternary operator requires two operands to operate

Syntax:

#include<iostream.h>

void main() { int a, b,c; cout<<"Enter a and b values:"; cin>>a>>b;

c=a>b?a:b; cout<<"largest of a and b is "<<c; } Enter a and b values:1 5 largest of a and b is 5

7. BIT WISE OPERATORS : C supports special operators known as bit wise operators for manipulation of data at bit level. They are not applied to float or double. operator meaning

& Bitwise AND

^ <<

~

Bitwise exclusive OR left shift right shift one's complement

Bitwise AND operator (&) The bitwise AND operator is a binary operator it requires two integral operands (character or integer). It does a bitwise comparison as shown below:

Shift Operators The shift operators move bits to the right or the left. These are of two types:

. •Bitwise shift right operator . •Bitwise shift left operator