Jump to content

Objects and Classes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
josh

josh

    Newbie

  • Members
  • Pip
  • 3 posts
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


#2
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

josh said:

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.

josh said:

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.

josh said:

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.

josh said:

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?

#3
josh

josh

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks...... next time i will try a better one....

#4
arymania

arymania

    Newbie

  • Members
  • Pip
  • 1 posts
There are ebook for this material? Let me know.

#5
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
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?