Unit-1 CPP Document PDF

Summary

This document provides a high-level overview of object-oriented programming (OOP) concepts in C++. It covers topics such as data abstraction, encapsulation, inheritance, and polymorphism. The document is suitable for undergraduate-level computer science students.

Full Transcript

##### UNIT-1 ##### Object A Object B 1. Emphasis is on doing rather than procedure. 2. Programs are divided into what are known as objects. 3. Data structures are designed such that they characterize the objects. 4. Functions that operate on the data of an object are tied together in...

##### UNIT-1 ##### Object A Object B 1. Emphasis is on doing rather than procedure. 2. Programs are divided into what are known as objects. 3. Data structures are designed such that they characterize the objects. 4. Functions that operate on the data of an object are tied together in the data structure. 5. Data is hidden and can't be accessed by external functions. 6. Objects may communicate with each other through functions. 7. New data and functions can be easily added. 8. Follows bottom-up approach in program design. ##### 1. Objects 2. Classes 3. Data abstraction and encapsulation 4. Inheritance 5. Polymorphism 6. Dynamic binding 7. Message passing ##### OBJECTS ##### CLASS: - Classes are **blue prints of objects**. - It is a collection of objects of similar type. - For example, mango, apple and orange are members of the class fruit. - Classes are user-defined data types and behave like the built-in types of a programming language. - If fruit has been defined as a class, then the statement, fruit mango; will create an object mango belonging to the class fruit. - For class, no memory is allocated. ##### DATA ABSTRACTION : ##### DATA ENCAPSALATION : - The insulation of the data from direct access by the program is called **[DATA HIDING]** or **[INFORMATION HIDING]**. ##### INHERITENCE : ##### ##### ![](media/image6.png) ##### ##### ##### ##### ##### POLYMORPHISIM: ##### DYNAMIC BINDING : ##### MESSAGE PASSING : An object oriented program consists of **a set of objects that communicate with each other**. ##### Employee. Salary (name) ##### Message ##### ##### ![](media/image9.png) ##### BENEFITS OF OOP: 1. Through inheritance we can eliminate **redundant code** and extend the use of existing classes. 2. We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of **development time and higher productivity**. 3. This principle of data hiding helps the programmer to build secure programs that can't be invaded by code in other parts of the program. 4. It is possible to have multiple instances of an object to co-exist with out any interference. 5. It is **easy to partition** the work in a project based on objects. 6. Object-oriented systems can be easily **upgraded** from small to large systems. 7. Message passing techniques for communication between objects makes the interface description with external systems much simpler. 8. Software **complexity can be easily** managed. ##### APPLICATION OF OOP: 1. Real -- Time systems. 2. Simulation and modeling 3. Object oriented databases. 4. Hypertext, hypermedia and expert text. 5. Al and expert systems. 6. Neural networks and parallel programming. 7. Decision support and office automation systems. ##### Structure Of A C++ Program : ##### ##### ##### // my first program in C++ ##### ##### \#include \ ##### ##### using namespace std; ##### ##### cout \ - **Logical expressions**: Logical Expressions combine two or more relational expressions and produces bool type results.\ **Examples**: a\b && x\y   - **Pointer expressions**: Pointer Expressions produce address values.\ **Examples**: &x, ptr, ptr++ where x is a variable and ptr is a pointer. - **Bitwise expressions**: Bitwise Expressions are used to manipulate data at bit level. They are basically used for testing or shifting bits.\ **Examples:** C++ Expression Note: An expression may also use combinations of the above expressions. Such expressions are known as compound expressions. - ### **Special Assignment Expressions** Special assignment expressions are the expressions which can be further classified depending upon the value assigned to the variable. - **Chained Assignment** Chained assignment expression is an expression in which the same value is assigned to more than one variable by using single statement. Example: a=b=20     or    (a=b) = 20   - **Embedded Assignment Expression** An embedded assignment expression is an assignment expression in which assignment expression is enclosed within another assignment expression. Example In the above code, we have declared two variables, i.e., \'a\' and \'b\'. Then, we applied embedded assignment expression (a=10+(b=90)). - **Compound Assignment** A compound assignment expression is an expression which is a combination of an assignment operator and binary operator. Example, a+=10;    **Operator overloading:** An operator is overloaded in this type of polymorphism to give it the user-defined semantics. Function overloading and operator overloading are two terms used in C++ to describe the ability to specify multiple definitions for a function name or an operator in the same scope. Operator Overloading is **the method by which we can change the function of some specific operators to do some different task**.  ### Unary Operators and Binary Operator overloading **Unary operators:** - Operators which work on a single operand are called unary operators. - Examples: Increment operators(++), Decrement operators(--),unary minus operator(-), Logical not operator(!) etc... **Binary operators:** - Operators which works on Two operands are called binary operator. #### #### **Operator Overloading in Unary Operators** We can overload a unary operator like any other operator. We can redefine the unary operators to behave in a certain way on certain operands using unary operator overloading in C++. It is basically used for operating on user-defined datatypes, like classes and structures. **Example:** \#include\ using namespace std; class UnaryOverload { int hr, min; public: void in() { cout\hr; cout\min; } void operator++(int) //Overload Unary Increment { hr++; min++; } void operator\--(int) //Overload Unary Decrement { hr\--; min\--; } void out() { cout\

Use Quizgecko on...
Browser
Browser