Sie sind auf Seite 1von 22

INTRODUCTION TO C++

C++ is an Object Oriented Language it's an extension to C

OBJECT ORIENTEDPROGRAMMING CONCEPT


General-Purpose programming language Encapsulation with Data Abstraction (data hiding) Type Extensibility Inheritance Polymorphism with Dynamic Binding Development of OOP software Uses C's efficiency and portability

C++ AN OBJECT ORIENTED APPROACH


C++ is better than C. Class & Objects Encapsulation & Type extensibility Containership Inheritance & Polymorphism Dynamic Binding Exception Handling

CLASS & OBJECTS


Object : An object is something that has state, exhibit some well defined behavior and has a unique identity. Class : may be defined as a set of objects that share structure and share common behavior.
BIRDS

PEACOCK

SPARROW

KINGFISHER

OWL

NEW KEYWORDS INTRODUCED IN C++


asm
const_cast inline private type id

bool
delete namespace protected throw

catch
explicit new this virtual

class
friend public try typeid

CLASS
A Class has a structure as follows :
class class_name { member function 1 (); member function 2 (); member function n (); };

FUNCTIONS
Functions are of 4 kinds : No return no argument : void hello(); No return vid argument : void hello(int x); Return with argument : int hello (int x); Return with no argument : int hello();

OBJECTS
Objects are the pre instance of a class. An object acquire all the properties that a class has. Objects are created as : Class_name Object_name; Likewise the functions defined in class are called through this object. Object_name. member function;

INBUILT CLASSES INTRODUCTION


io_base ios istream ostream iostream

ios_base : The ios_base class is designed to be a base class for all of the hierarchy of stream classes, describing the most basic part of a stream, which is common to all stream objects. Therefore, it is not designed to be directly instantiated into objects. ios : Both this class and its parent class, ios_base, define the members of all standard streams that are independent of whether the stream object is an input or an output stream. istream : istream objects are stream objects used to read and interpret input from sequences of characters. Ostream : ostream objects are stream objects used to write and format output as sequences of characters.

TYPECASTING
Conversion of mathematical data types from one to another.

THE :: OPERATOR (SCOPE RESOLUTION)


Mainly used to access a global variable within a local scope.

NAMESPACES
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. They are written as : Namespace xyz { int a,b,c; } And this namespace variables can be called in the program as : xyz :: a; Also keyword using can be used to specify the namespace: Eg: using xyz :: b;

FUNCTION OVERLOADING
In C++ two different functions can have the same name if their parameter types or number are different.
#include <iostream> //using namespace std; int operate (int a, int b) { return (a*b); } float operate (float a, float b) { return (a/b); } int main () { int x=5,y=2; float n=5.0,m=2.0; cout << operate (x,y); cout << "\n"; cout << operate (n,m); cout << "\n"; return 0; }

INLINE FUNCTION
An inline function is used to suggest to the compiler that the code generated by the function body is inserted at each point the function is called, instead of being inserted only once and perform a regular call to it. The syntax of inline func. Is : Inline function_name (arguments...) {instructions...}

ACCESS SPECIFIERS
The access specifier determines the access to the names that follow it, up to the next access specifier is declared. There ae 3 access specifiers : Public Private Protected

STATIC VARIABLE & STATIC FUNCTION


A static variable is a variable that has been allocated statically and whose lifetime extends across the entire run of the program. A static function is that which can access only static variables

FRIEND FUNCTION
In order to access non-public members of a class outside we use friend function.

CONSTRUCTOR & DESTRUCTOR


The constructors are used to initialize objects and destructors are use d to delete them. A constructor has the same name as that of the class in which they are created. A destructor also has the same name as that of a class but prefixed with ~

<STRING.H>

INHERITANCE

Dynamic_cast class CBase { }; class CDerived: public CBase { }; CBase b; CBase* pb; CDerived d; CDerived* pd; pb = dynamic_cast<CBase*>(&d); // ok: derived-to-base pd = dynamic_cast<CDerived*>(&b); // wrong: base-to-derived
const_cast This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order to pass a const argument to a function that expects a non-constant parameter: // const_cast #include <iostream> using namespace std; void print (char * str) { cout << str << endl; } int main () { const char * c = "sample text"; print ( const_cast<char *> (c) ); return 0; }

POINTERS

REFERENCE
A reference is similar to a pointer. But has subtle difference from a pointer. Eg: int i=10; int &j = i;

Das könnte Ihnen auch gefallen