C++ NOTES

234
OBJECT ORIENTED PROGRAMMING Conventional Programming using high level languages such as COBOL, FORTRAN and C is commonly known as procedure oriented programming. In the procedure oriented approach, the problem is view as a sequence of things to be done such as reading, calculating and printing. A number of functions are written to accomplish these tasks the primary focus is on function. Procedure oriented programming basically consists of writing a list of instructions for computer to follow and organized those instructions to groups known as functions. While we concentrate on the development of functions, very little attention is given to the data that are being used in various functions. 1 C++ MAIN PROGRAM FUNCTION 3 FUNCTION 2 FUNCTION 1

Transcript of C++ NOTES

C++ OBJECT ORIENTED PROGRAMMING Conventional Programming using high level languages such as COBOL, FORTRAN and C is commonly known as procedure oriented programming. In the procedure oriented approach, the problem is view as a sequence of things to be done such as reading, calculating and printing. A number of functions are written to accomplish these tasks the primary focus is on function. MAIN PROGRAM

FUNCTION 1

FUNCTION 2

FUNCTION 3 Procedure oriented programming basically consists of writing a list of instructions for computer to follow and organized those instructions to groups known as functions. While we concentrate on the development of functions, very little attention is given to the data that are being used in various functions. In a multi function program, many important data items are placed as global, so that they may be accessed by all the functions. Each function may have its own local data.

1

RELATION BETWEEN FUNCTION AND PROCEDURE ORIENTED PROGRAMMING.

DATA

IN

THE

The major motivative factor in the invention of object oriented approach is to solve some of the drawbacks in the procedural approach. Object oriented Programming treats data as a critical element in the program development and does not allow to flow freely around theFUNCTION1 LOCAL DATA FUNCTION 2 LOCAL DATA

system. It ties data more closely to the function that operate on it and protects it from accidental modification from outside functions. OOP allows decomposing a problem in to a number of entities called objects and then builds data and function around these entities. The data of an object can be accessed only by the functions associated with that t object. However function of an object can access the functions of other objects. ORGANISATION DATA & ORIENTED PROGRAMMING. Object ADATA FUNCTIONS

FUNCTIONS

IN

OBJECT

object BDATA FUNCTIONS

OOP is the most recent concept in the programming. It is therefore important to have a working definition of OOP. OOP is an approach that provides a way of modularity programs by creating partitioned memory area for both data and functions. That is an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access that data. CONCEPTS OF OBJECT ORIENTED PROGRAMMING

2

OBJECTS: Objects are the basic runtime entities in an objectoriented system. When a program is executed, the objects interact by sending messages to one another. CLASS: Class is a data type (user defined) to declare an object. That is objects are variables of type class. Once a class has been defined, you can create any number of objects belonging to that class.

DATA ABSTRACTION AND ENCAPSULATION Placing of data and functions into a single unit (class) is known as encapsulation. The data is not accessible to the outside world and only those functions which are placed in the class can access it, these functions provide the interface between objects data and the program. The insulation of data from direct access by the program is called data hiding. Abstraction refers to the act of representing essential features without including the background details and explanations. Classes use the concept of abstraction and defined as a list of abstraction attributes. Since the classes use the concept of data abstraction they are known as abstract data types (ADT). INHERITANCE : Inheritance is the process by which objects of one class acquire the properties of objects of another class. In OOP the concept of inheritance provides the idea of reusability. This means that we can add additional features of an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. POLYMORPHISM : Polymorphism means the ability to take more than one form. It places an important role on allowing objects having different internal structures to show the same external interface. This means that a general class of operations may be accessed in the same manner eventhough specific actions associated with each operation may differ.

3

DYNAMIC BINDING : Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at the runtime. It is associated with polymorphism and reference depends on the dynamic type of that reference.

C++C++ is an object oriented programming language, initially named as "C with classes". C++ was developed by Bjarne Stroustrup at "AT&T" bell laboratories in the early of 1980's. In C++, the features of C and simula 67 are combined. There fore C++ is an extension of C with a major addition of the class construct feature of "Simula 67". Since the class was major addition to the original major C language, it is called as "C with classes". However later in 1983, the name was changed to C++. The idea of C++ comes from the 'C' increment operator "++", there by suggesting that C++ is an incremented version of 'C'. C++ is a superset of C. Therefore almost all C programs are also C++ programs. The three most important facilities that C++ adds on to 'C' are classes, function overloading and operator overloading. These features enables us to create abstract data types. Inherit properties from existed data types and support polymorphism. Thus making C++ a truly object oriented language.

4

STRUCTURE OF A C++ PROGRAM : The programming structure of C++ is almost same as "C" except the class concept.INCLUDE FILES Class Declarations Class Functions Definitions main Function : It is a header files consists of the entities (objects) related to input and output. "cout >" is known as extraction or getfrom operator. It takes the value from the keyboard and assigns it to the variable on it's right. "COMMENTS" : (//) C++ introduces a new comments symbol "//". Comments start with a double slash (//) symbol and terminate at the end of a line. A comment may start anywhere in the line and what ever follows till the end of the line is ignored.

5

Syntax :

// comment

Example For A C++ Program # include # include void main() { clrscr(); cout>eno; cout>eadd; cout>acno;

printf("%s",sopt[1]); gotoxy(50,12); printf("%s",sopt[2]); gotoxy(50,13); printf("%s",sopt[3]); textcolor(BLACK); textbackground(WHITE); gotoxy(50,row); cputs(sopt[i]); normvideo(); ch=getch(); switch(ch) { case 72: //UPARROW KEY (ASCII CODE) IS 72 row--; row=row13?11:row; break; case 13: //ENTER KEY flag=0; } i=row-10;

";

111

gotoxy(15,12); coutacname; gotoxy(15,14); cout