PROGRAMMING IN C++

PROGRAMMING IN C++

 

  • C++ was developed by bjarne stroustrup at AT&T bell lab in USA.
  • It is enhancement of C language with new feature.
  • C++ is object oriented programming language.

 

  • C++ is similar like C language it also start with main function.

It is a free form language.

  • Structure of C++ program
  1. Include file (header)
  2. Class declaration
  3. Class function member
  4. Main function.
  • Tokens:- It is the smallest unit of a C++ program
  1. Keyword
  2. Identifier
  3. Operator.
  • Keyword:- Are those word which have pre-defined mean in C++. The list of keyword in C++.

Double, int, float, ASN, NEW, switch, Auto, else, operator, template, Break, enum, private, this, case, extern, protected, throw, catch, try, char, for, register, typedef, class, friend, return, union, const, goto, short, unsigned, continue, if, signed, virtual, default, inline, sizeof, void, delete, static, volatile, do, while, long, struct.

  • Data type in C++:- C++ allows all the data type of C language only one additional data type Boolean is additional or representing the Boolean value.
  • I/P O/P function in C++:- In every C++ program we need certain i/p and expect certain o/p so we must no about i/p o/p function in C++.

There are two library function available in C++ for i/p and o/p.

  • ­Cin- It is a library function which is use to read data from the keyword it is similar to scanf in c language.

SYNTAX
Cin >>variable 1;
Example: int X;
Cin >> X

  • Cout- It is a library function in C++ which is use to print data on screen (console).

SYNTAX
Cout << variable;
Example:- int X;
out << “enter value of X”
Cin >> X;
Cout << cx;

  • Concept of OOPS:- (object oriented program)
  • Object
  • Class
  • Data Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Message Passing
  • Class:- Class can be defined as wrapping of data and member together. In a single building block. Where data is known as data member and function is known as member function. Which perform some operation data. It is a user a data type similar in structure but the difference is that structure
  • Declaration of class:- A class declaration contain to part:-
  1. Class head
  2. Body of class
  • Class head:- A class consist of keyword class followed by the class name. which is valued by C++. The class head is followed  by the body of class consist of data member and member function in close with the part of curly brass.

Syntax of the class

Class Class name
{
Access specifier data member
Access specifer member function
};

  • Class scope:- All the data member and member function inside are belong to class scope each class have it own scope starting at the left brace followed by body of space and ends with corresponding right brace.

EXAMPLE

Class X
{
Private : int X;
Float y;
Public:
Void i/p _ data();
Void display _ data ();
}

From Open curly brace to closed curly brace are scope of the class.

  • Nested Class:- A class with in a class called nested class both outer & inner class follow the access rule.
  • Access control specifier:- One of the important in C++ is data hiding or encapsulation which be access even by mistake by any function outside the class.

In C++ data hiding can be reached by using 3 access control specifier.

They are:-

  1. Public
  2. Private
  3. Protected

This 3 Keyword are used to specific the 3 level access protection from hiding data member and member function.

  • Private:- Private member of class can be access by member function of same class in which it declared. The private member implement OOPS concept of data hiding.

Example:-

Class student
{
Private : int roll no;
Void display ()
{​
Cout << roll_­no;
}
};
Void main()
{
Student S1;
S1. Roll no = 25; || error
S1.display(); || error
}

 

  • Public:- Public member function are represented keyword public & can be access anywhere within the program either by function at belong to the class or by those external classes.

Example:-
Class student
{
Public : int roll no
Public void display ()
{
Cout << roll_no;
}
};
Void main()
{
Student S1;
S1.roll no= 25;​
S1.display();
}

 

  • Protected:- Protected member of class are similar to prime number there protected can be access by member function of same class as well as it is declared

Example:-|
Class student
{
Protected : int > roll no;
Void display ()
{
Cout << “enter text”
Cin > roll_no;
}
};
Void main()
{
Student S1,
S1. Roll no = 20;
S1. Display();
}

 

  • Dynamic object:- Object at have are created during runtime is called dynamic.
     
  • Pointer object:- Pointer variable is use to store the any address the variable .
  • Pointer is use to store any object.
  • Constructor and Deconstructor:- Most important object oriented program is support for initialized object when they are created and destroying them when no longer time need a class C++ should have two special member internal working of class these function are.
  • Constructor and Deconstructor:- A constructor is special member function whose task to initialize the object of this class. The constructor have same name of in the class and it existed in own class.
  • Important properties of constructor:- Constructor cannot be overloaded.
  • Constructor can be use initialization the object
  • It is possible to created without constructor in such case a dummy constructor is called at run time.
  • A constructor that cannot value is called default object.
  • It name is same as the name of class.

DECLARATION OF CONSTRUCTOR

Class Class name
{
Access variable
Specifier
Public: constructor name()
{
}
};

  • Types Of Constructor:-

In C++ there are 3 of constructor are available:-

1)Default constructor
2) Parameterized constructor
3) Copy constructor

Default Constructor:- If a class do not have any constructor define compiled created default constructor.

 Example:

Class testing
{
Private: int a,b;
Public: testing();
{
Cout<< “ a new object has been created
<< end 1
a=0;
b=0;
}
void display()
{
Cout<< “the variable a= “<a<<end 1
Void main()
{
Testing t ;
t. display()
}

Parameterized constructor:- A constructed main take argument and parameter such constructer are called Parameterized constructor.

Example:

# include <iostream.h>
Class ABC
{
Int x, y;
Public:
ABC (int a, int b)
{
X=a;
Y=b;
}
void sum
{
int c;
C=x+y;
Cout << c;
}
};
Void main()
{
Int a, b;
Cout << “enter two value”;
Cin>>a>>b;
ABC obj;(a,b);
Obj. sum();
getch();
}

Copy constructor:- A copy constructor in an object from another object it copy value of one object to another.

Example:

Class code
{
int id;
Public : code (int a)
id = a;
}
Code (code and x)
{
id = x.id;
}
Void display()
{
Cout <<id;
}
};
Void main()
{
Code A (100)
Code B (A);
Cout << “value of a” <<A. display()<<end1
Cout << “value of b” <<B. display()<<end1
getch();
}

 

  • Deconstructor:- A deconstructor special number function use to destroy a function the object which is no longer is use. The deconstructor is automatic in hope before the program turn the terminated. Live the constructor name of deconstructor also same name but it present tilde(~).

A Deconstructor take no arguments and was not run time. If no user define deconstructor exist through class and one needed and the complied implicitly declare a deconstructor in deconstructor is also declare as public member of class.

Example:

Class X
{

Private : data member
Public: X():- Constructor
{
}
~*():- Deconstructor
{
}
}

Characterstics:

  1. The name of the deconstructor is same as class name but prefixed by tilde
  2. Tilde the deconsturctor from the constructor the same clas.
  3. Desconstructor cannot take any argument and do not return any value.
  4. Deconstructr destroy created before terminated the object.
  5. Over loading of a descontructor cannot be possible. So, each class should have any one descontructor.

Class X
{
Private: int X
Public : X ()
{
Cout<< “object has been created”>>
~*()
{
Cout<< “object has destroyed”,
}
Void display()
{
Cout<< “the value of x “<<x
}
};
Void main()
{
X. Object;
Obj. display()
getch();
}

 

  • Data Abstraction:- It can be define as act of representing arsenical feature of any object without include background detail or explanation.

For example:- A television which we can turn on and off change the channel, adjust the values and add external components such as speaker, VCR, and DVD player. But we do not known internal mechanism of the television we are only bother about display on the screen.

  • Inheritance:- It is the process of driving the property of new class from old class where new class is called derive class and old class is called base class.

C++ supports different type of inheritance:-

  1. Single Level
  2. Multi Level
  3. Multiple Level
  4. Hybrid
  5. Hierarchical

Inheritance help C++ to support the concept of reusability. C++ class can be use several ways the class once has been return and tested it can be adopted by other program. A soot their requirement this is basically by creative new class, reusing the properties of existing process.

Example: Class A :- Base Class / Parent Class
Class B:- Derived Class / Child Class

 

  • Single level inheritance:- Derivation of new class from a class base class is called single level inheritance. It is like parent and child relationship here only two class are link to each other. 
    Example:

Class A:- Base Class / Parent Class

Class B :- Derived Class / Child Class

  • Multi-Level inheritance:- In this type inheritance , there are number of level and it has used in those cases were we want to use all property in number of level according to the requirement. Example:

    Class A: Class B: Class B: Class C and soon

    Class A
    Class B
    Class C

  • Multiple Inheritance:- Provide class with the ability to derived from any number of classes. Thus there is ! derived class and there can be any number of base classes. If there are more than one base class. It is known as multiple inheritance.
     
  • Hierarchical Inheritance:- it is the mechanism of derived a new class by in inheriting by the feature of more than 1 class use to derived some other new class may be inherited there will be only 1 waste class called super class from which all other class are derived.A sub class created by inheriting the property of the base class and adding it own.
     
  • Hybrid:- It is a mechanism in which more than 1form of inheritance is used in designing the class here a new class created from multiple and multi level classes or it could be created from the multiple and hybrid classes.
    Hybrid inheritance is the combination one or more form of inheritance.

     
  • Inline function:- C++ in line function is a powerful concept that’s commonly use in classes. If a function is declare inline the compile phase is copy of code of that function at which point where the function is called compile time.
    To inline a function a function place the keyword inline before the function; function name and define the function before any call are made to the function. It is request not command so the compiler may ignore inline qualifier the basic purpose is to save time and cost and space. Example:-

#include <iostream.h>
Inline void hello()
{
Cout<< “Hello”;
}
Void main()
{
hello()
}

  • Advantages:-
    1) It increase performances
    2)It save time
    3)It save space

     
  • Disadvantages:-
  1. It is request not a command so compiler not accept.
  2. If someone use to many keyword it may increase size of the program.
  3. The speed benefit get reduce if the function grow in size.
  • Situation:- In which inline function may not work properly
  • Function returning values; haling the loop; switch or go to the exit.
  • If the function getting static variable.
  • If inline function are recursion.

Friend Function:- We know that private member of a class can be exist only by the member function of the same class. But there are some cases where the private member need to be excess by member of other class.
This can be achieved by declaring a non-member function as friend function.

#include<iostream.h>
Class sample
{
Int a, b;
Public: void set value()
{
A=25;
B=40;
}
Friend mean (sample & S)
};
Float mean (sample & S)
{
return float (s.a+s.b)12.0;
}
Void main ()
{
Sample X;
X. set value;
Cout<< “mean value” <<mean(x);
}

  • Characteristics of friend function:- A function should be declare are using the keyword friend
  • It should be placed only in function declaration of friend function.
  • It should be declare either in public & private section of a class.
  • When a class declared as friend the friend class as access to the private data of this class that made this a class.
  • It can be called just like normal C++ function.
  • Dot operation can be used to access the indicial data member.

Syntax

Class Class name 
{
Variable;
Public:
variable
friend function name()
};

  • Static Data Member:- Whenever an object is created each object contains it own copy of data member where as the member function is shared by all the object of C class there are some case where we need maintain single data item all the object of the class can be shared it can be achieved by declaring the data item as static. Where static is a keyword.
  • Characteristics of static data member:-
  • Whenever static data member is starting it as only single only copy. It will be shared by allocated object of the class.
  • It should be declared keyword static within a class.
  • Access control from static data member is as data member.
  • When first object of class is created the data memory is initially to zero.

Example:-

#include<iostream.h>
Class car
{
Static
Private: int Cout;
Public : void display();
};
Int car :: Count ();
Void car :: display()
{
Cout<< “the value of static data member”<<Count;
Void main()
{
Car x;
X. display();
}

  • Dynamic memory allocation:- Dynamic memory allocation is memory management technique in which memory is allocated at runtime from the heap.

Sometime we are not aware how much memory will be need to store particular info. So then we use dynamic memory management method to get the required amount of memory. For this purpose C++ provide to operators.

  1. New
  2. Delete

New Operator:- We can allocate memory at runtime within heap for the variable of a given type using a special operator called new operator which return the address of  the space allocated this operator is called new operator & syntax of declaration is 

Pointer = new type
Pointer = new type [ number of element]
e.g int * x;
x = new int [5];

 

In this example X is a pointer of integer type in second statement new  operator will allocated memory for 5 elements by using new operator the memory address of the allocated memory block is assigned to X pointer.

  • Delete operator:- When we no longer need dynamic memory allocated using new operator we use delete operator which allocate memory previously allocated by new operator.

Syntax:-

Delete pointer;
delete [] pointer

 

first statement releases the memory of a single element allocate using new operator.

And the second statement allocated the memory allocated for array of element using new and a size in () bracket.

Category: 

tags: 

Share

Who's new

  • ravirajpatil871...
  • shubhambajoria
  • yassir
  • demiholyman890954
  • scottgillum51169040

Get Notified

 

Share

We are Social

Syndicate

Subscribe to Syndicate