×
YOU DID NOT WAKE UP TODAY TO BE MEDIOCRE.
--Your friends at LectureNotes
Close

Note for Object Oriented Programming Using Cpp - OOP By SATYAM SUMAN

  • Object Oriented Programming Using Cpp - OOP
  • Note
  • 11 Topics
  • 21 Views
  • Uploaded 10 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

CHAPTER – 1 INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) C++ What is “C++”? C++ is an object oriented programming language. In other words we can say that it is a Hybrid (i.e. all features of “C” is directly implemented with “C++”) programming language or it is superset of “C”. It adds a number of object oriented features such as Objects, Inheritance, function overloading and operator overloading to C. These features enable building of programs with clarity, extensibility and no difficulty to maintenance. C++ can be used to build a variety of systems such as editors, compilers databases, communication system, and many real life complex application systems. A typical C++ programs would contains four basic sections namely : Include file section, class declaration section, function declaration section and main declaration section. BASIC CONCEPTS OF OOPs Object oriented programming language have developed in an evolutionary manner and have become very popular over past few year. C++ is one such language. It is necessary to understand some of the concept of used extensively in OOP. These include: 1. Object 2. Classes 3. Inheritance 4. Encapsulation ADVANTAGE of OOP OOP offers several benefits to the program designer and the user. • Through inheritance, we can eliminate redundant code and extend the use of existing classes. • The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program. • It is possible to have multiple instances of an object to co-exist without any interference. • It is easy to partition the work in a project based on objects. • It is possible to map objects in the problem domain to those in the program. • Software complexity can be easily managed. COMPARISION OF PROCEDURAL PROGRAMMING AND OOPs When we need to read data item in an object, we call a member functions in the object. The function will access data and return us the value. we cannot access data directly, because data is hidden and cannot be altered accidentally. Data and its functions are said to be encapsulated into a single entity. When we need to modify the data in an object, we need to know the functions that interact with it. Thus we use that function. This procedure simplifies in writing, debugging and maintaining a program. A C++ program thus consists of number of objects which communicate with each other by calling one another’s member functions. DEFINITION OF CLASS AND OBJECT Class: - The concept of class is best understood with an analogy. Class is the way to define/declare the attribute (Instance Variable) and functionality of an object. Object: - An object is the something that has a fixed shape or well defined boundary. In the ordinary sense, an Object is something which is capable of being seen. A object can be any one or all of the following: (a) A visible thing (b) Something that can be picked up by a person (c) Something towards which thought or action is directed CONCEPT OF INHERITANCE AND ENCAPSULATION Inheritance:-This feature of c++, supports re-usability means that existing class/old class can be extended by a new class. New class is called derived class and old class, on which a new class is created, is called base class. Encapsulation: - It is mechanism that associates the code and the data it manipulates into a single unit and keeps them safe from external interference and misuse. In c++, this is supported by a construct called class. An instance of class is known as an object, which represents a real word entity. COPYRIGHT@MANOJ COMPUTERS,INDRAPURI,PATNA Page 1

Text from page-2

C++ OPERATOR OVERLOADING ‘C++’ provides feature to overload mostly arithmetic, relational and other operators. Overloaded means operator can be used in c++ program for addition work not existing task. For example existing function of – (minus) is to negate the digit. But we can use this operator to negate an object (i.e. the additional task of minus operator). Statements to perform additional task, are written under special function namely operator ( ), i.e. provided by c++. Syntax to use operator function: Return type operator <operator sign> (argument) { } DYNAMIC BINDING It means that the code links with program call is not known until the time of call at run time. Dynamic link libraries include predefined functions that are linked with application program when it is loaded dynamically, instead of when executable file is generated statically. It is also known as late binding. BASIC PROGRAM CONSTRUCTION Program statements: - C++ has the following types of program statement: (i) Declaration statement (ii) Assignment statement (iii) Function call statement (iv) Return statement Comments – Comments can be included in a program in three different ways in C++ as given below: Type Usages /* */ All characters between /* and */ are ignored. // All characters after the // up to end of the line are ignored. CHAPTER – 2 ELEMENTS OF C++ LANGUAGE TOKENS & IDENTIFIERS Tokens – Token is the smallest element of a program that is meaningful to the compiler. When we submit a java program to the java compiler, the compiler goes through the text and extracts individual tokens. Tokens can be categorized into the following five types: (a) Identifier (b) Keywords (c) Constants (d) Strings (e) Operators (a) Identifiers: – Identifiers refer to the names of variables, functions, arrays etc. given by a programmer. These are also the names of language objects, which can take many values, one at time. A variable is a name that the program associates with a storage location in memory. The following are the rule to name a Identifier:→It can consist of any sequence of letter, digits and underscore (_).→The first character should be a letter or an underscore. →No other special characters are allowed. →Reserved words (Keyword) cannot be used as name of identifier. →It is case sensitive language. (b) Keywords – The words which are reserved to do specific tasks at time of designing a programming language is known as Keywords. Following are the keywords: auto double int continue for virtual struct break else Long default goto catch switch case enum Register do if private typedef char extern Return asm new try union const float Short operator this inline unsigned template static Signed throw delete volatile void protected class Sizeof friend public while COPYRIGHT@MANOJ COMPUTERS,INDRAPURI,PATNA Page 2

Text from page-3

C++ Character set: - There are two character sets in c++ language. These are (a) Source characters: - Using source characters, the source text is created. Example - Alphabets A to Z and _(underscore), Decimals 0 to 9, Special characters + - * ~ # % etc. (b) Execution characters/escape sequences: - These are interpreted at time of execution. The value of execution characters are defined as per the implementation. C++ provides the mechanism to get such characters that are invisible or difficult through execution characters. Execution characters and escape sequences are used interchangeably. Following are the execution characters: Execution Character Meaning Result at execution time \0 End of string Null \n End of a line Moves the active position to the initial position of the next line. \r Carriage return Moves the active position to the initial position of the next paragraph. \f Form feed Moves the active position to the initial position of the next logical page. \v Vertical tab Moves the active position to the next vertical tabulation position. \t Horizontal tab Moves the active position to the next horizontal tabulation position. \b Backspace Moves the active position to the previous position on the current line. \a Alert Produces an audible alert \\ backslash Presents with a backslash \. VARIABLES AND CONSTANTS Variable: - A variable is a name that our program associated with a storage location in memory. After declaring a variable within a program we can assign it a value. (b) Constants – Constants are refer to fixed values that do not change during the execution of a program. Constants are the following types: (i) Backslash character constant: - The backslash (\) alters the meaning of the character that follows it. Following are the backslash character string and their meanings: Backslash Character Meaning Backslash Character Meaning \a Produces an audible alert \r Prints a carriage return. signal. \b Moves the curser back one \t Prints a horizontal tab. space. \f Moves the cursor to the next \v Prints a vertical tab. page. \n Print a new line. (ii) Numeric constant : - It has a constant numeric value assigned to it. The value of the constant can be positive or negative numeral. There are two types of numeric constant: ▪ Integer Constant: - Integers constant are whole numbers and do not have any fractional or decimal part. ▪ Floating-Point Constant: - The constants, which are used to representing very large or very small numeral values and fractions is known as floating-point constants. (iii) String constant: - A sequence of zero or more characters surrounded by double quotes is called string constant. This can be also defined as an array of character constants. (iv) Symbolic constant: - It is a constant that is represented by name (symbol) in the program. It make a program more readable and protect it from side effects created by variable. In c++, there are two ways of creating symbolic constant: ▪ const: - We can declare a C++ constant by writing const before the identifier’s data type. For example: - const int a = 10; ▪ enum: - An enumerated data types provides a way for attaching names to numbers. In C++, enum automatically enumerates a list of words by assigning them values 0, 1, 2 and so on. For example: height verylarge; Here verylarge is of the type height. COPYRIGHT@MANOJ COMPUTERS,INDRAPURI,PATNA Page 3

Text from page-4

C++ Dynamic initialization of variables: - Dynamic initialization is mainly used in OOPL. In C we initialize a variable before using it. But in C++, we can declare and initialize a variable simultaneously at the place where the variable is used for the first time. it means, We initialize a variable at run time. Example: Float average = sum/t; Reference Variables: - A reference variable provides an alternative name (alias) for previously defined variable. Syntax: datatype & reference-name = variable-name Examples: - int amount = 100; int & total = amount; cout<< amount << total; DATA TYPES The kind of data that a variable may hold in a programming language is called the data type. There are two reasons for distinguishing among data types. These are: ▪ The compiler may use the proper internal representation for each type of the data. ▪ The programmer designing the programs may use proper operators for each type of data. The data types can be classified into the following three categories: (a) User defined data type: - User defined data type enables a programmer to invent our data types and define what values it can take on. (b) Derived Data Type: - Derived data types are built from the basic integer and floating point data types. The array data type is one example of derived data type. (c) Built-in (Basic) Data Types: - The three built-in data types available in C++ are: ▪ Integral Type – This can be further classified into: →int – int is the basic integer data type. It is treated as an integer in that it cannot hold fractional values. →char – This is a data type which can hold both the character data or the integer data. For example – char c; ▪ Floating Type – This can be further classified into: →float – float integers are not adequate for representing very large values of numbers and fractions. For this we need floating-point types of data representation. →double – the word double stands for double precision. It is capable of representing a real number ranging from 1.7x10-308 to 1.7x10308 hich gives twice as much precision as represented by a float. ▪ Void Type – The void data type has two important purposes. →A function does not return a value and the other is to declare a generic pointer. Example – void func (a,b) →This informs the compiler that any attempt to use the returned value from func() is a mistake and should be flagged as an error. For example – func (x,y) Array: - Array is the collection of same types of elements with unique name. array allocates contiguous space in memory subscript or index of an array is started with 0 and up to size-1. Array is a data structure (Static). Data structure enables us to store and manipulate organized collection of data. If we have to handle more the one elements of same type for common purpose, then we prefer array rather creation of individual variable to store and manipulate data. For Example: int x[3]; Array can be of: • Single Dimension • Double Dimension String: - String is the collection of characters thus we can say that it is example of character array. Declaration of Single Dimension Array: Syntax: Data type <Array Name> [Size] Declaration of Double Dimension Array: Syntax: Data type <Array Name> [Row][Column] OPERATORS An operator operates on one or more variables and performs an action. It consists of words or symbols. Example: +, -, /, * The Operators can be classified into following categories: (i) Arithmetic Operator (ii) Relational Operator (ii) (iii) Boolean Logical Operator (iii) (iv) Assignment Operator COPYRIGHT@MANOJ COMPUTERS,INDRAPURI,PATNA Page 4

Lecture Notes