Page 1 :
UNIT – 1, Introduction, 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. c++ is structure(procedure) oriented and, object oriented programming language., The file extension of C++ program is “.CPP” .Function overloading and, operator overloading are possible. Variables can be declared in inline i.e, when required, In c++ more emphasis is give on data rather than procedures., Polymorphism, encapsulation and inheritance are possible., Data abstraction property is supported by c++. 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., Dymanic binding is supported by C++. It supports all features of c language., It can be called as an incremental version of c language., , Procedure Oriented Programming Paradigm:, Procedure oriented programming basically consist of writing a list of, instruction or actions for the, computer to follow and organizing these instruction into groups known as, functions., , The disadvantage of the procedure oriented programming languages is:, 1. Global data access, 2. It does not model real word problem very well, 3. No data hiding, , Copyright @Programming Space Chhattisgarh
Page 2 :
Characteristics of procedure oriented programming:, 1. Emphasis is on doing things(algorithm), 2. Large programs are divided into smaller programs known as functions., 3. Most of the functions share global data, 4. Data move openly around the system from function to function, 5. Function transforms data from one form to another., 6. Employs top-down approach in program design, Languages used in Procedural Programming:, FORTRAN, ALGOL, COBOL,, BASIC, Pascal and C., , Object Oriented Programming Paradigm:, “Object oriented programming as an approach that provides a way of, modularizing programs by creating partitioned memory area for both data, and functions that can be used as templates for creating copies of such, modules on demand”., , Copyright @Programming Space Chhattisgarh
Page 3 :
Features of the Object Oriented programming, 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, , Basic Characteristics of object oriented programming, •, •, •, •, •, •, , Class, Object, Data Abstraction, Encapsulation, Polymorphism, Dynamic Binding, Copyright @Programming Space Chhattisgarh
Page 4 :
• Message Passing, , Class :, A group of objects that share common properties for data part and some, program part are collectively called as class., In C ++ a class is a new data type that contains member variables and, member functions that operate on the variables., Syntax :, Class classname{, //some data, //some function, }, Example :, class Room {, public:, double length;, double breadth;, double height;, double calculateArea(){, return length * breadth;, }, double calculateVolume(){, return length * breadth * height;, }, };, , Object :, Objects are the basic run-time entities in an object-oriented system. They, may represent a person, a place, a bank account, a table of data or any item, that the program must handle., The fundamental idea behind object oriented approach is to combine both, data and function into a single unit and these units are called objects., , Copyright @Programming Space Chhattisgarh
Page 5 :
The term objects means a combination of data and program that represent, some real word entity., For example:, consider an example named Amit; Amit is 25 years old and his salary is, 2500., The Amit may be represented in a computer program as an object., The data part of the object would be (name: Amit, age: 25, salary: 2500), The program part of the object may be collection of programs (retrive of, data, change age, change of salary). In general even any user –defined typesuch as employee may be used. In the Amit object the name, age and salary, are called attributes of the object., Syntax :, class class-name, {, data-member;, public:, data-member;, member-function;, } object1, object2,.....n;, Example :, #include <iostream>, #include <string>, using namespace std;, class MyClass {, public:, int myNum;, string myString;, };, Copyright @Programming Space Chhattisgarh
Page 6 :
int main() {, MyClass myObj;, myObj.myNum = 15;, myObj.myString = "Some text";, cout << myObj.myNum << "\n";, cout << myObj.myString;, return 0;, }, Output :, 15, Some text, , Data abstraction :, Abstraction refers to the act of representing essential features without, including the back ground details or explanations. Classes use the concept, of abstraction and are defined as size, width and cost and functions to, operate on the attributes., , syntax, class-keyword class-name, {, private:, //all private members here...., public:, //all public members here......., };, , Copyright @Programming Space Chhattisgarh
Page 8 :
Encapsulation:, The wrapping up of data and function into a single unit (called class) is, known as encapsulation. The data is not accessible to the outside world and, only those functions which are wrapped in the class can access it. These, functions provide the interface between the objects data and the program., , Example :, #include<iostream>, using namespace std;, class test, {, private:, int x;, public:, test(int a), {, x =a;, }, int get(), {, return x;, }, };, int main(), {, test a(8);, cout<<"The Number is: "<<a.get();, return 0;, }, , Output:, Copyright @Programming Space Chhattisgarh
Page 9 :
The Number is: 8, , Inheritance :, Inheritance is the process by which objects of one class acquire the, properties of another class. In the concept of inheritance provides the idea, of reusablity. This mean that we can add additional features to an existing, class with out modifying it. This is possible by desining a new class will, have the combined features of both the classes., , Syntax, class derive-class-name : visibility-mode base-class-name, , यह ाँ visibility-mode, access specifier को दर् ात है । जो private, public और, protected प्रक र क हो सकत है ।, #include<iostream.h>, #include<conio.h>, #include<stdio.h>, class first, {, protected:, int a,b;, public:, void get_num(int x,int y), {, a = x;, b = y;, }, };, class second:public first, {, public:, void get_sum(), {, int sum;, sum = a+b;, cout<<"Total: "<<sum;, }, };, , Copyright @Programming Space Chhattisgarh
Page 10 :
void main(), {, int a,b;, clrscr();, second obj;, cout<<"Enter two number: ";, cin>>a>>b;, obj.get_num(a,b);, obj.get_sum();, getch();, }, , OUTPUT, Enter two number: 5 6, Total: 11, , Polymorphism and overloading:, Polymorphism means the ability to take more than one form. An operation, may exhibit different instance. The behaviour depends upon the type of, data used in the operation. A language feature that allows a function or, operator to be given more than one definition. The types of the arguments, with which the function or operator is called determines which definition, will be used. Overloading may be operator overloading or function, overloading. It is able to express the operation of addition by a single, operater say ‘+’. When this is possible you use the expression x + y to, denote the sum of x and y, for many different types of x and y; integers ,, float and complex no. You can even define the + operation for two strings to, mean the concatenation of the strings., , Type of Polymorphism, 1. Compile time polymorphism, 2. Run time polymorphism, , Copyright @Programming Space Chhattisgarh
Page 12 :
output: 70, , Static binding:, #include<iostream>, using namespace std;, class Base {, public:, void display() {, cout<<" In Base class" <<endl;, }, };, class Derived: public Base {, public:, void display() {, cout<<"In Derived class" << endl;, }, };, int main(void) {, Base *base_pointer = new Derived;, base_pointer->display();, return 0;, }, इसका आउटपुट –, In Base Class, , Dynamic binding:, Binding refers to the linking of a procedure call to the code to the executed, in response to the call. Dynamic binding means the code associated with a, Copyright @Programming Space Chhattisgarh
Page 13 :
given procedure call is not known untill the time of the call at run-time. It is, associated with a polymorphic reference depends upon the dynamic type of, that reference., , Example :, #include <iostream>, using namespace std;, class A {, public:, void final_print() // function that call display, {, display();, }, void display() // the display function, {, cout<< "Printing from the base class" <<endl;, }, };, class B : public A // B inherit a publicly, {, public:, void display() // B's display, {, cout<< "Printing from the derived class" <<endl;, }, };, int main(), {, A obj1; // Creating A's pbject, Copyright @Programming Space Chhattisgarh
Page 14 :
obj1.final_print(); // Calling final_print, B obj2; // calling b, obj2.final_print();, return 0;, }, , Output:, Pr Printing from the base class, , Printing from the derived class inting from the base class, Printing from the base class, Printing from the base class, , Message passing:, An object oriented program consists of a set of objects that communicate, with each other. A message for an object is a request for execution of a, procedure and therefore will invoke a function (procedure) in the receiving, object that generates the desired result. Message passing involves, specifying the name of the object, the name of the function (message) and, information to be sent., , Benefits of OOPS:, • Reusability: In OOP‟ s programs functions and modules that are, written by a user can be reused byother users without any, modification., • Inheritance: Through this we can eliminate redundant code and, extend the use of existing classes., • Data Hiding: The programmer can hide the data and functions in a, class from other classes. It helps the programmer to build the secure, programs., • Reduced complexity of a problem: The given problem can be, viewed as a collection of different objects. Each object is responsible, for a specific task. The problem is solved by interfacing the objects., This technique reduces the complexity of the program design., • Easy to Maintain and Upgrade: OOP makes it easy to maintain and, modify existing code as new objects can be created with small, differences to existing ones. Software complexity can be easily, managed., Copyright @Programming Space Chhattisgarh
Page 15 :
• Message Passing: The technique of message communication, between objects makes the interfacewith external systems easier., • Modifiability: it is easy to make minor changes in the data, representation or the procedures in anOO program. Changes inside a, class do not affect any other part of a program, since the only public, interface that the external world has to a class is through the use of, methods., , Applicaton of OOPS:, The most popular application of oops up to now, has been in the area of user interface, design such as windows. There are hundreds of windowing systems developed using oop, techniques., Real business systems are often much more complex and contain many more objects, with complicated attributes and methods. Oop is useful in this type of applications because it, can simplify a complex problem. The promising areas for application of oop includes., 1. Real – Time systems., 2. Simulation and modeling, 3. Object oriented databases., 4. Hypertext,hypermedia and expertext., 5. Al and expert systems., , Copyright @Programming Space Chhattisgarh