728x90 AdSpace

Latest Article



C++ Programming Classes basic Intro Tutorial with Example





Online C++ Programming what is Classes and how to use classes in c++ basic Introduction  Tutorial  with Example of constructor public private example with code 

CLASSES

                 You learned how to use "structures" to group data elements into a single meaningful entity (theoretically functions may also be incorporated into structures but this is not practiced). Now you will be using classes to group data elements and functions into single entities. The data elements are referred to as "data members" and the functions as "member functions". The instances of a class are called "objects". Classes, just like structures, are like blue prints and the instances are real blocks, each having all the features of the blue print. This explains the fact that memory is allocated only when an instance of a class (an object) is declared and not when the class definition is provided. Each object of a class has its own copy of data members but all the objects share a single copy of the member functions.
                 Now we'll look at a class definition and discuss various features of a class. A class can be defined by using the keyword class.
        class  demo                    // keyword class tells that demo is a class
        {
               private:
                        int pvtData ;
               public:
                       int pubData;
                        void show( ) { cout<<"\nPrivate Data  " }
        };                             //semicolon at the end of the class definition

What do private and public mean?

                 Private and public keywords are member access specifiers. A class's private members are normally not accessible outside the class. (The dot operator doesn't help even). These members are accessed indirectly through the public members functions of the same class. Public members, however, may be accessed outside the class by using the dot operator. Generally member functions are public and data members are private but this is no rule. Data members may be public and member functions may be private.
Members of a class are private by default i.e. they are treated as private if no access specifier is explicitly stated. Always state the access specifiers explicitly.

How to Access the Members of a Class?

                 Both data members and data functions of a class can be accessed by using the dot operator (similar to the way members of structures are accessed) provided they are public. In the following example we'll declare an instance of the demo class defined above in our main program and then access its members by the dot operator.
PROGRAM

OUTPUT
 
void  main ( )
{
   demo first ;
   first.pubData=5 ;
   // first.pvtData=5; will generate an error
   cout<<"\nPublic data is : "<<first.pubData ;
   first.show( ) ;
};
 

Public data is : 5
Private data 
 
 
 
 
3 At this point you may be rightly wondering about the use of private data when they cannot be initialized. The next section will answer your questions.

What is a Constructor?

                 A constructor is a special member function that has no return type (even void is not used) and has the same name as that of the class. When an object (an instance) of a class is created, the constructor is called automatically for the object by the compiler. This automatic calling makes the constructor an excellent choice for initializing the data members of the class.
        class  demo                   // keyword class tells that demo is a class
        {
           private:
                   int pvtData ;
           public:
                   int pubData;
                    demo( ) { pvtData=10; }     //note that constructor has no return type
                    void  show( ) {  cout<<"Private Data is : "<< pvtData }
         };
Now the same main program that we discussed above will have the following output.
   Private Data is : 10

no image
  • Title : C++ Programming Classes basic Intro Tutorial with Example
  • Posted by :
  • Date : 06:37
  • Labels :






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment