Вы находитесь на странице: 1из 32

C++ NOTES

EVALUATION OF OOP: The invention of the object oriented technology eliminates the flows faced because of procedure oriented approach. The oop approach binds the data and functions that work on it together. This secure data from any changes from outside the module. The system is divided into numerous invisible entities called objects. Objects ARE INDEPENDENT OF EACH OTHER. They are responsible for managing their own state and offering services to other objects. DEFINATION: OBJECT ORIENTED PROGRAMING IS 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. Benefits of oop: 1. The principle of data hiding helps the programmer to build secure programs. 2. It is easy to partition the work in a project based on objects. 3. Object oriented systems can be easily upgraded from small to large systems. 4. We can eliminate redundant code and extend the use of existing classes.

5. Oop provides an advantage in production and maintenance of software. 6. Object oriented program involves the identification and implementation of different classes of objects and their behavior. 7. Software complexity can be easily managed. 8. It is possible to have multiple instance of an object to coexist without any interface. 9. Oop leads to saving of development time and higher productivity. APPLICATIONS OF OOP: Artificial intelligence and expert system. Simulation and modeling studies. Object oriented database systems. Object oriented operating systems. Real time systems. Office automation systems. Cad / cam systems. Multimedia applications. Graphical user interface (GUI). Computer based training and education systems.

OBJECT ORIENTED LANGUAGE:

A language that is specially designed to support the oop concepts makes it easier to implement them. The language should support several of the oop concepts to claim that they are object oriented. Depending upon the features they support, they can be classified into the following two categories: 1. Object based programming languages. 2. Object oriented programming languages.

OBJECT ORIENTED PROGRAMING IS THE, style of programming with the features data encapsulation, data hiding and access mechanisms, automatic initialization and clear up of objects, operator overloading. Languages that support programming with objects are said to be object based programming languages. OBJECT ORIENTED PROGRAMING incorporates all of object based programming features and dynamic binding. Languages that support these features include C++, Smalltalk and object Pascal.

PROPERTIES/ CONCEPTS/ BUILDINGS OF OOPS: Objects Classes

Inheritance Polymorphism Dynamic binding Overloading Data abstraction Encapsulation

OBJECTS: objects are basic run time entities in an objectoriented prgmng. An object may be defined as an identifiable entity with some Characteristics and behaviour. Each of these has a define state, behaviour and identity. The features of an object are. (i) State

(ii) Behaviour (iii) Identity

CLASSES: The most important feature of object oriented programming is the Classes. A class is the way to binf the data and its

associated functions together. It allows the data to be hidden if necessary from the external use.

Inheritance: Inheritance is one of the most powerful features of OOPS .inheritance is a process of creating a new class from existing class. Polymorphism Polymorphism comes from the Greek words ploy and morphism. poly means many and morphism means forms i.e many forms In oops polymorphism refers to identically named methods have different behaviours depending on the type of object. Polymorphism means the ability to take more then one form.

Dynamic Binding: Binding refers to tie up of a procedure call. Dymanic binding means that the code associated with a given procedure call is not known until it call at runtime. Overloading: Overloading is a language features that allows a function or operator to be given more than one definition. The type of the argument with which the function or operator is called overloading.

Data Abstraction: Creating a new datatype using the encapsulated item that are suited to an application to be programmed is known as data abstraction. Th edata types created by the data abstraction is known as abstract data type(ADT).

Encapsulation The wrapping of data and function into a single unit is known as encapsulation. Data encapsulation is the most important feature of a class. By using encapsulation we can accomplish data hiding.

INTRODUCTION TO C++

C++ is an object oriented programming language. Initially named C with classes . C++ was developed by BJARNE STORUSTUP at AT & T Bell labs in 1980. C++ is a versatile language for handling very large programes. It is suitable for virtually any programming task including

development of editors ,compilers ,databases,communicayion systems and any complex real life applications.

DIFFERENCE BETWEEN C AND C++:


C 1. No data security 2. Non reliability of programs 3. Top down design approach 4. Function overloading and operator overloading are not possible in procedure oriented languages. 5. Polymorphism, encapsulation, and inheritance are not possible in procedure oriented languages. C++ 1. Data security 2. Reliability of programs 3. Bottom up design approach. 4. Function overloading and operator overloading are possible in object oriented languages. 5. Polymorphism, encapsulation, and inheritance are possible in procedure oriented languages.

STRUTURE OF C++ PROGRAM: A typical C++ program would contain four sections as shown in figure. These sections may be place in separate code files and then compiled independently of jointly.

Include files Class declaration

Class functions definitions

Main function program

It is common practice to organize a program into three separate files. The class declarations are place in a header files and the definition of member functions go into another file. This approach enables the programmer to separate the abstract specification of the interface from the implementation details. Finally, the main program that uses the class is placed in a third file which includes the previous two files as well as any other files required.

Structure of a C++ program

[Preprocessors] [Global declaration] [Linkage section] void main() { [<Declaration_Part>]; [<Execution_Part>]; }

Comments Comments are those lines which are ignored by the compiler.

C++ supports 2 types of comments

1). Single line comment (//) 2). Multiline comment (/*.*/) Input/output Statements

C++ supports input/output with the help of streams.

1). Cout 2). Cin

Cout: Syntax:

Used for display message and memory values.

Cout<<[Message]<<var1<<var2<<..<<var_n

Cin: Used for accept data from input device (keyboard).

Syntax:

Cin>>var1>>var2>>>>var_n

Keywords/ Reserved words:

Keywords are those words whose definition is already defined in a compiler.

List of keywords: Auto asm static int float double long short int signed unsigned break Continue goto return void switch if Else struct union enum extern typedef while do for char const far huge volatile mutable sizeof case default C++ keywords: Class public private protected virtual inline try catch throw new delete type Friend . Drawbacks of structured programming Data gets freely move in a program. More priority is given to procedure/function rather than data. Perfect code reusability is not provided. To overcome the above listed drawbacks foundations were laid with a programming technique called Object Oriented Programming System(OOPS). Using oops

->Data is restricted. ->Equal priority is given to data and procedure ->Perfect code reusability is provided.

Components of Oops Following are the 2 component on which oops dependent. Often they are called Building Blocks of oops. 1.class 2.object class: It is a user-defined data type. Class is a collection of data members and member functions. Class is enclosed with 3 access specifiers. Private Public Protected

Syntax:

Class [<class_name>] { private: data members; member functions; friend functions; Public: data members; member functions; friend functions; protected: data members; member functions; friend functions; };

Points to be remembered to work with class:

Class is created with a keyword called class class name to a class is optional class can be created as local or global. More than one class can be created in a single program but always with unique class names Instance variables cant be assigned with constant values directly in a class No sequence is followed for access specifiers of a class Access specifier can be used any no.of times By default data members and member functions of a class are private. Every class should be terminated by semicolon(;) Methods enclosed in a class cant be provided with looping constructs,switch case statemens,recursive funs..etc. because by default methods are inline functions Object:

Instance of a class is refers to object. (or) It is the one through which instance variables and methods of a class gets access.

Note:

Minimum of one object is must Redeclaration of object is invalid Object created at one class the same cant be used in other class For every object separate memory is allocated Object can be created in the following procedure a). class <class_name> <obj1,[obj2,..]>; b). <class_name> <obj1,[obj2,..]>;

egs.. Accessing procedures for 3 access specifiers of a class.

1.when instance variables and methods are created at public access specifier, then they get access in and out side of the class. 2.when instance variables and methods are created at private access specifier they work only in side a class. 3.minimum of one public method is must to interact with private access specifier 4.protected access specifier is equal to private access specifier 5.they can share the data among themselves

i.e. private can access public,public can access private and protected etc.

Defining methods outside a class

By default methods enclosed in a class works for inline, where it cant be expanded by writing looping constructs,switch case statements. To expand the method it should be defined outside the class. To define the method outside the class operator called (:: - Scope resolution operator) is used.

Note: when method is defined out side the class then its prototype or declaration should be created in a class.

Syntax: Return type class_name :: method_name(list of par) {

// body of the method.. } Friend Functions

Data stored in private instance variables can be accessed with member functions and non-member functions of a class. A keyword called friend should be provided before the return type of a function.when a friend function is created its prototype should be created in a class at any access specifier when a definition of a friend function is written outside the class then it should be written without scope resolution operator and friend function should be called without object.

Syntax: Friend return_type method_name(list of parameters)

Friend Class

Data stored in private instance variables of one class can be with the methods of the different class, where it is essential that a class should be made as friend to that class. Syntax: Friend class <class_name>; (or) Friend class_name; INLINE FUNCTION Inline functions in c++ are used like macros. That is they will be substituted at the point of the function call at the time of compilation rather that making a function definition will be preceded by the keyword inline. Syntax: Inline return_type func_name([parameters]) { //function body } Advantages:

1) 2)

reduces the overheads raised in function calls increases the modularity of the program

3) code will be substituted to the point of call at the time of compilation. 4) Faster execution of the prgm.

5) Inline function can be used in the code just as normal functions.

LIMITATIONS: 1) the function should be very simple.

2) The function should not have a loop,a switch,a goto statements. 3) The function should not have a empty return statement if it doesnt return something. 4) The function shouldnt be recersive.

PASSING OBJECTS TO FUNCTIONS:

Like any other data type , an object may be used as a function arguments . the object can be done in 3 ways

1) A copy of the entire object is passed to the function. 2) Only the address of the object is transferred to the function 3) Retuning objects from the function

The first method is called as pass-by-value .since a copy of the object is passed to the function , any changes to the object inside the function do not effect the object used to call the function. The second method is called pass-by-reference.when an address of the object is passed ,the called function works directly on the actual objects used in the call. This means that any changes made to the object inside the function will effect in the actual object. RETURNING OBJECTS: A function can not only receive the objects as arguments but also can return them. The example prgm shows how an object can created and returned to other function.

Operator Overloading:

It is a concept of polymorphism where poly refers to many And morphism refers to forms, i.e it is a concept of many forms. By overloading an operator, operator can be provided to perform multiple tasks. A keyword called operator is used before an overloaded operator. Syntax:

return_type operator <operator to be overloaded>(list of parmeters) { // Body of the method. }

Function overloading
Overloading refers to the use of the same thing for different purpose. C++ also permits overloading of function. This means that we can use same function name to create functions that perform a variety of

different tasks. This is known as function polymorphism in oops.

It is a concept of polymorphism
CONSTRUCTORS: Contructors are special methods of a class which are use to construct thee data for an instance (object) when it is created. The main operation of constructors is to allocate the required resources such as memory and initialize the objects of its class. Characteristics of constructors: 1) It has the same name as that of the class name which it belongs 2) It is executed automatically when ever the class is instantiated(object is created) 3) It does not have any return type(not even void)

4) It is normally used to initialize the data members of a class 5) It is also used to allocate memory to the dynamic data members of a class.

TYPES OF CONSTRUCTORS 1) 2) 3) default constructor parameterized constructor copy constructor

Default Constructor: Constructor with no parameters refers to default constructor Syntax: Class-name () { Stmt 1; Stmt 2;
. .

Stmt n; } 1) Contructors should always be created with public access specifier 2) Constructors support to make different expressions for calculations

3)

Constructors can call other methods of a class

PARAMETERIZED CONSTRUCTOR: Constructors with parameters refers to parameterized constructors ,more than one parameterized constructor can be enclosed in a single class where it differentiates them based on list of parameters. It is often called as constructor overloading Class-name(list of parameters) { Stmt 1;

Stmt n; } DYNAMIC CONSTRUCTOR Allocation of memory during the creation of objects can be done by constructors too.

The memory is saved as it allocates the right amount of memory for each object. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. New operator is used to allocate memory.

OVERLOADED CONSTRUCTOR The other name of multiple constructors in a class is overloaded constructor. They are Constructor() //no arguments Constructor (int, int) //two arguments Constructor (int, float, int) // three arguments

DESTRUCTORS: Destructors are the special methods of a class which provides a confirmation about the memory which is destroyed created for an object. Syntax: ~class-name () { Stmt 1;

Stmt n; }

ARRAY OF OBJECTS: An array can be of any data type including struct. We can also have arrays of variables that are are of the class type. Such variables called as array of objects. EX: POINTER TO OBJECTS: Pointer can be used to hold the address of objects .the need for using pointer to objectsbecome clear when objects are to be created while prgm is being executed. Syntax: Class-name *ptr-obj; Where class name is the name of class and *ptr-obj is the name of the pointer to the object of the class. A pointer variable can be defined to hold the address of an object which is created statically or dymanically as shown. Prt-obj=&object Where ptr-obj is the name of the pointer to the object and object is the address of object created.

This*
It is a keyword provided by c++ It is by nature of class type It is hidden objects which automatically enter into the class when a message is passed. It accesses instance variables with the help of arrow (->) operator.

It is used to differentiate between instance variables and local variables when represents with same variable names.

Dynamic Memory Until now, in all our programs, we have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the execution of the program. But, what if we need a variable amount of memory that can only be determined during runtime? For example, in the case that we need some user input to determine the necessary amount of memory space. The answer is dynamic memory, for which C++ integrates the operators new and delete. Operators new and new[]

In order to request dynamic memory we use the operator new. new is followed by a data type specifier and -if a sequence of more than one element is required- the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated. Its form is: pointer = new type pointer = new type [number_of_elements] The first expression is used to allocate memory to contain one single element of type type. The second one is used to assign a block (an array) of elements of type type, where number_of_elements is an integer value representing the amount of these. For example: int * bobby; bobby = new int [5]; Operators delete and delete [] Since the necessity of dynamic memory is usually limited to specific moments within a program, once it is no longer needed it should be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of the operator delete, whose format is: delete pointer; delete [] pointer;

The first expression should be used to delete memory allocated for a single element, and the second one for memory allocated for arrays of elements. REFERNCE VARIABLE: Refernce variable provides a different name for already defined variable.this is possible with the help of the address operator &. If we have an integer variable sum declared and initialized as follows Syntax: Data_type &refernce_var=var_name; Int &x=10;

TEMPLATES: Templates in c++ is called generic data types ,where generic refers to non-specific or universal.template supports to create functions where those functions are called gereric functions.

Templates is a technique that allows using a single function or class to work with different data types .using templates a single function can process any type of data i.e., formasl arguments of template function are of template type.

Template are divided into two types

1) Function template 2) Class template

Guidelines for template 1)templates should always be created as global 2) templates variable should be created of class type enclosed under < > 3) templates variable can be used anywhere in generic function i.e., can be used at formal parameters ,declaring local variables can be used as return type. 4) templates can be attached with max of one function & one class. 5) if other function of as program to be created as generic functions Must that templates should be used. 6) the process of changing formal parameters with different data types depends on the sending parameters .it is often called as instantiating of an object

function templates:

A generic function defines set of operations that will be applied to various types of data. A generic function has the type of data that it will operate upon passed to it as a parameter.

Syntax: Template <class type> Return_type function_name(parameter list) { //. Body of function //. } Class templates A class created by a user can be attached with a keyword called template . where it becomes generic class. Instances variables enclosed in a class can be of non-secific type. Template variable can be used anywhere in a class. It should be attached with template. Type of data to be stored in an instance variable trough an object should be specified between class-name & object enclosing it under < > Syntax:

Template <class T>

Class <class_name> { Private: Protected: Public: }

Вам также может понравиться