Monday, 12 September 2016

Class and Object

Class

Class is the collection of data member and member functions.A class is declared with the keyword class and class body enclosed by curly braces. A C++ program can have many number of classes.

Class is user defined data type
Defining the class

class  class_name
{
         data member
         member functions
};

Example:
class student
{
     private:                                 //access attribute
        int r_no;                                
        char name[20];                  //data members
      public:                                 //access attribute
        void get_data();
        void put_data();                //declaration of member functions
};

In above example student is the name of class. int r_no and char name[20] are data types declared and they are the private member of the class. Class members can access each other and they are grouped under two sections private and public, which are visibility labels.

Private and Public keywords

Public keyword specifies the public member of the class below its declaration.In above example function void get_data() and void put_data() are public members of the class and they can be accessed outside the class also.
Class members can be public or private and further we will see it can be protected also.By default if we do not specify any type class member would be private and private members are not accessible out side the class.
Class can hide the data with the help of private members.



Objects

Object is class type of variables.Declaration of the object is similar to any basic type of variable. Object are used to access the members of the class.

Class_Name Object_Name;

Example:

student s1;

where s1 is the object of the class student

Accessing class members
(.)member operator is used to access the class member like the structure variables,but only public members can be accessed using object of the class.
Private members can not be accessed out side the class

Example:

class test
{
    public:                   //access attribute
        int a;                              
        int b                  //data members
        void get_data()
       {
                a=10;
                b=20;
       }
        void put_data()
      {
               cout<<a;
               cout<<b;
       }              
};
 int main()
{
        student s1;           //object
        s1.put_data();      //accessing public member with object s1 and (.) member operator
        return 0;
}

Saturday, 10 September 2016

Introduction to C++

C++
C++ is a middle-level programming language developed by Bjarne Stroustrup in 1979 at Bell Labs.
C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.
C++ is known as a middle-level language, as it is having features of both high-level and low-level language features.
C++ is a superset of C,C++ is derived from C Language.
Object-Oriented Programming
C++ fully supports object-oriented programming, including the four pillars of object-oriented development:
·        Class
·        Object
·        Encapsulation
·        Data Abstraction
·        Inheritance
·        Polymorphism
·        Message Passing
Class
It is similar to structures in C language. Class can also be defined as user defined data type. Class contains data members and member functions in it. It declares & defines what data variables the object will have and what operations can be performed on the class's object.
Object
Objects are the basic run time entity. They are instances of class, which have data members and use various member functions to perform tasks. Object is also known as class type of variable.
Encapsulation
Encapsulation is the process to wrapping the data and functions into the single unit(class).It is one the most useful feature of the class. Data are not accessed outside the class and only function inside the class can access it. It is also called data hiding.
Abstraction
Abstraction is act of representing essential features without including the background details. We can create class as abstract class to perform this feature of abstraction.
Inheritance
Inheritance is the process of creating new class from the existing class. Existing class is known as base class and new class is called derived class. It is a way to reuse the code again and again. So when a class is derived from base class it can have some or all features of the base class and it can have its own feature too.
Polymorphism
One name multiple forms. With this feature we can create functions with same name but different arguments. So function name is same but definition and signature of functions is different.
Message Passing
Objects can communicate with each other. It requires creating classes, creating objects of classes and establishing communication between objects.
Benefits of object oriented programming
Code Reusability: Feature of inheritance allow to eliminate redundant code and extend the use of existing class
Data Hiding: It can help to build secure programs.
Message Passing: Message communication between objects makes interface simpler
It implements like real life scenario
It is easy to partition the work on the class and object base
Easily upgraded from small to large systems and easy to update and maintain the programs
Different between C and C++
C
C++
C is procedural oriented programming language
C++ is object oriented programming language
C is subset of c++
C++ is superset of c
C does not support data hiding
C++ supports data hiding
C is functional driven language
C++ is object driven language
Being procedure oriented programming language it has no support for polymorphism, inheritance, encapsulation
Being Object oriented programming language it has support for polymorphism, inheritance, encapsulation
C does not support function overloading and operator overloading
C++ support function overloading and operator overloading
C does not provide error handling
C++ provides error handling with “exception handling”
C does not have feature of class
In C++ data and functions are encapsulated together in class
C does not support reference variable
C++ supports reference variable

Different between Procedure Oriented and Object Oriented
Procedure Oriented
Object Oriented
C, VB, Fortron
C++,java,vb.net
Program is divided in to small parts called function
Program divided into parts called object
Functions are given more important than data
Data are given more important than functions
Data can move freely from function to function
Objects can move and communicate with each other
It follows top to bottom approach
It follows bottom up approach
It cannot hide the data
It can hide the data so more secure
It does not have any access specifier
It has acces specifier like public,private,protected
It is based on function only
It has various featured like overloading, encapsulation, abstraction, inheritance, message passing

Structure vs Class

Structure
Class
struct keyword is used to create structure
class keyword is used to create structure
Default access is public
Default access is private
Structure is group of data members
Class contains data members and member functions
Structure variable is used to access structure members
Object is used to access class members
Example:
struct student
{
        int no;
        char name[20];
}
Example:
class student
{
        int no;
        char name[20];
     public:
        void getdata();
        void putdata();
}