First C++ Class
Yogendra Pal
At the end of this tutorial you will be able to
• Write correct syntax of a class.
• Create data members and member functions of a class.
• Create objects of a class.
• Access the class members outside the class.
• Explain the memory representation of an object.
• Define functions inside and outside the class.
www.learnbywatch.com | yogendra@learnbywatch.com
Why create a class?
• class creates a user defined data type.
www.learnbywatch.com | yogendra@learnbywatch.com
General form of c++ class
www.learnbywatch.com | yogendra@learnbywatch.com
class class-name {
private data and functions
public:
public data and functions
};
Object Creation
www.learnbywatch.com | yogendra@learnbywatch.com
class class-name {
private data and functions
public:
public data and functions
};
class-name object-name;
class-name object1, object2;
Access class members
• The dot operator ( . )
• Required only if you are accessing a class member outside class.
www.learnbywatch.com | yogendra@learnbywatch.com
Define member function
www.learnbywatch.com | yogendra@learnbywatch.com
Inside class
return_type function_name(parameters)
{
function body
}
Outside class
return_type class_name::function_name(parameters)
{
function body
}
Scope resolution operator
Ask your questions
to learn better
Yogendra Pal
www.learnbywatch.com | yogendra@learnbywatch.com

Write First C++ class

  • 1.
  • 2.
    At the endof this tutorial you will be able to • Write correct syntax of a class. • Create data members and member functions of a class. • Create objects of a class. • Access the class members outside the class. • Explain the memory representation of an object. • Define functions inside and outside the class. www.learnbywatch.com | yogendra@learnbywatch.com
  • 3.
    Why create aclass? • class creates a user defined data type. www.learnbywatch.com | yogendra@learnbywatch.com
  • 4.
    General form ofc++ class www.learnbywatch.com | yogendra@learnbywatch.com class class-name { private data and functions public: public data and functions };
  • 5.
    Object Creation www.learnbywatch.com |yogendra@learnbywatch.com class class-name { private data and functions public: public data and functions }; class-name object-name; class-name object1, object2;
  • 6.
    Access class members •The dot operator ( . ) • Required only if you are accessing a class member outside class. www.learnbywatch.com | yogendra@learnbywatch.com
  • 7.
    Define member function www.learnbywatch.com| yogendra@learnbywatch.com Inside class return_type function_name(parameters) { function body } Outside class return_type class_name::function_name(parameters) { function body } Scope resolution operator
  • 8.
    Ask your questions tolearn better Yogendra Pal www.learnbywatch.com | yogendra@learnbywatch.com