Saturday, 26 July 2014

CLass and Object in C++ -1

Class is a way to bind data and its associated Function together .It also allows data and function to be hidden ,if necessary ,from External User .

The class Member that have been declare as private can be access only from within the class . on the other Hand Public data and function can be access from outside the class also .

By default the member of class is private . 


A simple Class Example :

  1. class test{
  2.                 //Private by default
  3.                 int a;   //Variable declaration
  4.                 float b;        
  5.         public: //declaring public data and func
  6.                 void assign(void); //declare member func
  7.                 void print(void);
  8. };

Creating An Object :

create a variable x of type "test" , In C++ Class variable is known as Object . Therefore  x is called an object of type "test" .

  1.     test x,y,x  //declaring class object

Here x y and z is object of type test

Accessing Class Member :-

private data of Class can be access only through the member function of that Class.

Public data and function can be access using "."operator  
Example :

   object-name.function-name(actual arguments)

  1.   test x; //create object
  2. x.assign() //access function using . operator
  3. x.print()

Defining Class Member function :

It can be define inside or outside the class .

member function define inside is always inline ( you mention or not ) .

Defining Member Function outside Class .




  •   return-type class-name :: function-name(arguments){
  •         function body

  • A C++ Program With Class :

    1. /*
    2.  Introduction to class in C++
    3.  http://beginer2cs.blogspot.com
    4. */
    5. #include<iostream>
    6. using namespace std;
    7. class test{
    8.         private: //declaring private data
    9.                 int a;
    10.                 float b;       
    11.         public: //declaring public data and func
    12.                 void assign(void); //declare member func
    13.                 void print(void);
    14. };
    15. /*
    16.  "::" called Scope resolution operator
    17. */
    18. void test::assign(){ //defining member func
    19.         int x;
    20.         float y;
    21.         cout<<"Enter value of a : ";
    22.         cin>>x;
    23.         cout<<"Enter value of b: ";
    24.         cin>>y;
    25.         a=x;
    26.         b=y;
    27. }
    28. void test::print(){
    29.         cout<<" a :- "<<a;
    30.         cout<<"\n b :- "<<b;
    31. }
    32. main(){
    33.         test x; //creating an object of class x
    34.         x.assign(); //calling public func of class test
    35.             /*we can't directly access private variable and data
    36.               using "." operator . only public function of same
    37.                   class can access it and modify it
    38.                 */
    39.         x.print();
    40. }

    Output :
    basic class in c++

    No comments:

    Post a Comment

    THANKS FOR UR GREAT COMMENT

    Blogger Widgets