C++

Derived from C Developed in 1983.

By: Bjarne Stroustrup
11-Nov-13 1

Origin of C++
C++ is an expanded version of C.The extension to C were first invented by Bjarne Stroustrup in 1980 at Bell Laboratories in Murray Hill,New Jersey. He initially called the new language “C with Classes”. However,in 1983 the name was changed to C++. Although C ,is one of the most liked and widely used professional languages in the world,the invention of C++ was necessiated by one major
11-Nov-13 2

Programming factor: Increasing complexity. C is an excellent programming language,it too has its limits.In C,once a program exceeds from 25,000 T0 100,000 lines of code,it become so complex that that is difficult to grasp as a totality.The purpose of C++ is to allow this barrier to be broken.The essence of C++ is to allow the programmer to comprehend and manage larger,more complex programs.
11-Nov-13 3

The object-oriented feature in C++,to use Stroustrup’s words, “Allow programs to be structured for clarity,extensibility,and ease of maintenance without loss of efficiency”. Although C++ was initially designed to aid in the management of very large programs ,it is in no way limited to this use. C++ shares C’s efficiency,much high-performance Systems software is constructed using C++.
11-Nov-13 4

OOPS
Object-oriented programming (OOP) is a new way of approaching the job of programming.Approaches to programming have changed dramatically since the invention of the computers,primarily to accommodate the increasing complexity of programs.When computers were first invented,programming was done by toggling in the binary machine instruction. The 1960 gave birth to structured programming.This is the method encouraged by languages such as pascal and C. The use of structured languages made it possible to write
11-Nov-13 5

even using structured programming methods a project becomes uncontrollable once it reaches a certain size. Object-oriented programming has taken the best ideas of structured programming and combined them with several powerful new concepts that encourage to approach the task of programming in a new way.Moderately complex programs fairly easily.However .break down a problem into subgroups of 11-Nov-13 6 . When programming in an object-oriented fashion.

All Object-oriented programming languages have three traits in common.Related parts that take into account both code and data related to each group.Organize these subgroup into a hierarchical structure is called Objects. 1.Objects 3.Encapsulation(Data Hiding) 11-Nov-13 7 .Class 2.

11-Nov-13 8 .

but an object has physical existence. ADT(Abstract Data Type).e. 11-Nov-13 9 . Thus.A class is a logical Abstraction.This new type is then used to declare objects of that class.CLASSES Classes are created using the Keyword Class.A class defines a new type that links code and data i.

11-Nov-13 10 .Functions and data declared within a class are private to that class and may be accessed only by other member of the class.Tips Access specifier: 1.Private 2.Public By default.

Encapsulation Encapsulation is the mechanism that binds together code and data.data may be private to the object and inaccessible to anything outside the object. An object provides a significant level of protection against some other. Within an object. Encapsulation allows the creation of an object.and that keeps both safe from outside interference or misuse.unrelated part of the program 11-Nov-13 11 .

Accidentally modifying or incorrectly using the private parts of the object. 11-Nov-13 12 .

Scope Resolution operator The :: operator is used to link a class name with a member name in order to to tell the compiler what class the members belong to.However.it can allow access to name in an enclosing scope that is “hidden” by a local declaration of the same name.the scope resolution operator has another related use. 11-Nov-13 13 .

11-Nov-13 14 . Static variable provide a global definition.This is done by redeclaring the static variable using the scope resolution operator to identify which class it belongs to.that is not allocating storage for it.Static variable Static variables are initialized to zero when the object is created.This causes storage for the variable to be allocated.when declaring a static variable within a class.

Unlike regular data members. No matter how many objects of a class are created.you are telling the compiler that one copy of that variable will exists and that all objects of the class will share that variable.When variables are declared as static. 11-Nov-13 15 .individual copies of a static member variable are not made for each object. only one copy of a static data member exists.

Static function Static function can have access to only to other static members(functions or variables) declared in the same class.A static member function can be called using the class name(instead of its object) 11-Nov-13 16 .

Since 11-Nov-13 17 .called an Inline function. Create short functions that are not actually called. rather their code is expanded inline at the point of each invocation.that is commonly used with classes. Inline functions are an important addition to C++ is that they allow to create very efficient code.Inline functions There is an important feature in C++.This process is similar to using a function like-macro .

when a function is expanded inline . It is best to inline only very small functions.Although expanding function calls inline can produce faster run times.The trouble is that instruction take time time.It is also a good idea to only inline those functions 11-Nov-13 18 .Classes typically require several frequently executed interface functions.a significant amount of overhead is generated by the calling and return mechanism.However.none of those operation occur. Each time a function is called.

11-Nov-13 19 .That will have significant impact on the performance of the program.

Template function are declared using the keyword.Template are blueprints of a function that can be applied to different data types.Templates A template function may be defined as unbounded set of overloaded functions.template.This means that all the possible parameters to the function are not known in advance and a copy of the function has to be created as and when necessary.The defination of the template begins with 11-Nov-13 20 .

The template keyword. 11-Nov-13 21 .This is followed by a commaseparated list of parameters types enclosed within the less than (‘<‘) and greater than (‘>’).

Arrays suffer from certain limitations.or an object of a class. A linked list is a chain of objects in which each objects consists of data and a pointer that stores the address (link) of the next logical object in the list.Linked List A data structure is a set of data.one way in which more than one object of the same data type can be stored in the memory is by using array.a structure variable. 11-Nov-13 22 .

2. Memory is allocated whenever required.and allocate memory for linked lists.Array manipulation is time-consuming and inefficient.inserting from linked list can be handled efficiently 11-Nov-13 23 . 2.insertion and deletion of data involves re-shuffling of array elements.1. The main advantage of linked list are: 1.It is not necessary to know the number of elments in advance. 3. Arrays are data structure of fixed size.

Single linked list 2.Circular linked list 11-Nov-13 24 . Type of Linked lists 1.Without having to restructure the lists.Double linked list 3.

A single linked list is a list of objects to the same data type. Start Info|Next 11-Nov-13 Info|Next Info|Next NULL 25 .the last node in the list will have a pointer containing NULL.An object in a linked list is called a node.Each object contains a pointer to the next object in the list.The list contains a pointer that points to the first object in the list.

which contains the address of the subsequent node(The next part of the node) The start pointer stores the address of the first node in the list. 11-Nov-13 26 .The second part is a pointer.The info part may consists of variable. 2.The first part contains the information(the info part of the node). 1.Each node has two parts.thus keep tracking of the beginning of the list.

the last node will not contain NULL. Start Info|Next Info|Next 27 Info|Next 11-Nov-13 .Circular Linked List In a circular linked list.start pointer point to the first node.Circular linked list used by the operating system for file buffering.instead it will point to starting node.like the single linked list.

which points to the next node.since there is only one pointer. 11-Nov-13 28 .Due to this traversal in the reverse direction is not possible.next.Double Linked List The single linked list and the circular linked list are called one-way lists.Due to the presence of only one pointer which points to the next node. in a double linked list traversal is possibe because two pointers prev. However.

Start NULL Prev|Info|Next Prev|Info|Next 11-Nov-13 29 .

Size operator(sizeof) 4. 3.scope resolution operator. 1. We can overload all the C++ operators except the following.conditional operator.Operator overloading Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators.Class member access operators. 11-Nov-13 30 . 2.

11-Nov-13 31 .The only difference being that a friend function requires two argument to be explicitly passed to it while member function requires only one.Friend function may be used in the place of member function for overloading a binary operator.

2. 4.New operators cannot be created.That cannot be overriden.only existing operators can be overloaded. 3.overloaded operators follow the syntax rules of the original operators.The overloaded operators must have at least one operand that is of user-defined type.Rules for overloading operators 1. 11-Nov-13 32 . We cannot use friend functions to overload certain operators. However member functions can be used.

But.• 5. Unary operators take no explicit argument and return no explicit values. When using binary operators overloaded through member function. . those overloaded by means of a friend function take one reference argument. • 7. the left-hand operand must be 11-Nov-13 33 object of relevant class. • 6. Binary operators overloaded through a member function take one explicit argument and those overloaded through friend function take two explicit arguments.

11-Nov-13 34 .Rules for overloading operators • 9. They must not attempt to change their own arguments. Binary arithmetic operators must explicitly return a value.

The stack is dynamic in nature. it constantly keeps changing its size due to the addition and deletion of data.Stacks Stack is a data structure in which operations like adding and removing are done from only one end. A stack is also called a push-down list. Therefore stack is form of single linked list in which addition and deletion are performed in the beginning of the list. 11-Nov-13 35 . referred to as top.

Adding item is known as push and deleting item is known as pop. This property is known as LIFO.Stacks In a stack insert and delete items from the position level top. 11-Nov-13 36 .

and elements are added at the other end. Queues are also called FIFO.Queue A queue is a data structure in which elements are deleted at one end. 11-Nov-13 37 .

11-Nov-13 38 .if it cannot. that the microprocessor’s register is used.the compiler will load the variable into a machine register.if possible.This keyword is just a request.Register variable The local variables that are used frequently in a program can be specified with the register.the remains in the RAM. A register is a place for data storage in the microprocessor.and not a demand.

11-Nov-13 39 .Register variables may increase the speed of a function if the variable selected are used frequently.

it would enter an 11-Nov-13 40 .Recursion A function can invoke another function.Otherwise.whenever a function invokes itself.Every time a recursive function calls itself.it does so with different values.All programming languages do not allow the use of recursive functions and procedure .it is called recursion.this is called function.

and 0!=1 11-Nov-13 41 .The function then recursively calls itself with different arguments. 2.called base values. n!=(n-1)!*n where n>1. Recursive procedure or function refers to itself.The function evaluates itself with the new arguments.and satisfies the following two conditions.for which the function will not call itself.the argument passed muse be closer to its base value.Infinite loop.Each time the function refers to itself.There must be certain values. 1.

11-Nov-13 42 .preprocessor The various instructions to the compiler in the source code of a c or C++ program.and although not actually part of the C or C++ language they expand the scope of C/C++ programming environment.These are called preprocessor directives.

#include #define #if #else #elif #define macro_name char-sequence 11-Nov-13 43 .The preprocessor contains the following directives.

The concept that binary tree follows is that of the binary searching where the set of data is divided into two parts and the data to be searched either in the first part or in the second part and so on.Binary Tree Binary tree is a hieracchical representation of data structure. 44 11-Nov-13 . This is used for to reduce the searching of the data.which reduces the speed by 1/8 that of the linear searching.

Binary tree ->In the binary tree maximum of two nodes can be attached with one node Root Left sub tree Right subtree 11-Nov-13 45 .

ROOT 11-Nov-13 46 .Ordered binary tree->the difference of the height of two subtree can be up to one.

• // file1. } 11-Nov-13 47 . f1().cpp • • • • • • • • • #include <iostream.h> int x=10. void main() { cout<< " in the main app ".y=20. void f1(). cout << x<< y.

cpp • • • • • • • #include <iostream.• //file2. } 11-Nov-13 48 .y. cout<< x<<y.h> extern int x. void f1() { cout << " in the function".

output is cdac delhi 2. output is All decks R 3.string b(str.char str[21]=“All decks Red alert”. 11-Nov-13 49 . cout<<a.String a(“cdac delhi”) cout<<a.28).String Manipulations 1. string a(str. cout<<b.11).

7). b.5). output is set t 6. 11-Nov-13 cout<<a. output is: Activat 50 .assign(“Activate the shields”. string b. cout<<b. cout<<a.string a. String a(5.assign(a.String manipulations 4. output is: ccccc 5.”c”).String a(“phaser set to stun”). a.7.