Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Objects and Classes


  • Please log in to reply
4 replies to this topic

#1 josh

josh

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 03 April 2010 - 08:03 AM

Structures help to group data elements, functions help to organise program that perform a task
Object of a class are similar to variables to structures. A class contain data items and member functions

The principal objective of object oriented programming is putting data and funtions into one class and data hiding.

A class is specified just like a structure with name. The body of the class is enclosed with curly braces and terminated with semicolon. Two keywords are used within the class viz. private and public.

By using keyword private in a class, we can hide data which can be accessed by functions defined within the class
By using keyword public in a class, both data and functions can be accessed by any functions even from outside the class

Generally data is private and functions are public. Reverse may also be true. Member functions need to be defined within the class specification just as inline in function are done. Space in memory is set up only when object are defined in the main() pgm and not during class specification. Member functions are accesisble only through the objects of that class using the class member access operator ".".
eg:
#include<iostream.h>
class sample
{
      private:
             int value;
      public:
             void memfun(int x)
             {
                   value=x;
             }
             void display()
             {
                   cout<<value;
             }
}; // NOTE: CLOSING BRACE AND SEMICOLON
 
void main()
{
        sample a1,a2; // 2 OBJECTS OF SAMPLE CLASS
        a1.memfun(1234);
        a2.memfun(6789);
        cout<<"\n HOME PO BOX no is :";
        a1.display();
        cout<<"\n OFFICE PO BOX no is :";
        a2.display();
}
 
 
OUTPUT IS
 
HOME PO BOX no is :1234
OFFICE PO BOX no is :6789


eg: A class with multiple data objects

#include<iostream.h>
class student
{
       private:
             int register_no;
             char grade;
             float fees;
       public:
             void assign(int x, char y, float z)
             {
                   register_no= x; 
                   grade= y;
                   fees=z;
             }
             void display()
             {
                   cout<<"\nRegister number :"<<register_no;
                   cout<<", Grade obtained :"<<grade;
                   cout<<", Fees :$"<<fees;
             }
};
void main()
{
         student s1;
         s1.assign(1234,'A+',50);
         s1.display();
}

OUTPUT

Register number :1234, Grade obtained : A+, Fees :$50

  • 0

#2 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 04 April 2010 - 10:58 AM

Structures help to group data elements, functions help to organise program that perform a task
Object of a class are similar to variables to structures. A class contain data items and member functions


Bad start, by reading this i assume that you don't know the difference between a struct and a class in C++, in fact, they are exactly the same thing, the only difference is: struct has data and functions public by default, class is private by default.

The principal objective of object oriented programming is putting data and funtions into one class and data hiding.


If it was just that, it wouldn't be invented.

A class is specified just like a structure with name. The body of the class is enclosed with curly braces and terminated with semicolon. Two keywords are used within the class viz. private and public.

By using keyword private in a class, we can hide data which can be accessed by functions defined within the class
By using keyword public in a class, both data and functions can be accessed by any functions even from outside the class


You didn't featured protected and friend keyword.

Member functions are accesisble only through the objects of that class using the class member access operator ".".


Or -> if it is a pointer...

Conclusion:

You didn't featured hundreds of things in this tutorial. Why write a tutorial like this, about this, if i can easily type "c++ classes" on google and get hundreds of tutorials much more informative than this?
  • 0

#3 josh

josh

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 April 2010 - 06:25 AM

Thanks...... next time i will try a better one....
  • 0

#4 arymania

arymania

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 14 April 2010 - 03:49 AM

There are ebook for this material? Let me know.
  • 0

#5 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 14 April 2010 - 03:55 AM

It seems you have edited your tutorial, which seems that is only worst than before... void main?

http://www2.research....html#void-main

Not to mention that your 2 new class examples are basically the same, they only differ in number of fields... what is the relevance in that?
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download