1 of 13

Classes & Objects

Mr. Santosh N. Nagargoje

2 of 13

Class

  • User defined data type which binds data and its functions together
  • Blueprint for creating objects
  • Defines properties and behaviour
  • Syntax:

class class_Name{

access_specifier:

Member_variable declaration( properties)

access_specifier:

Member_function declaration/definition(Behaviour)

};

3 of 13

Access Specifiers

  • public
    • Data can be accessed from anywhere outside the class
  • private
    • Data cannot be accessed from outside the class.
    • It can be accessed within the class only by using member functions.
  • protected
    • Data can only be accessed within the class and within the subclasses(child classes)

4 of 13

Class Example

class Person{

private:

string name;

int age;

double salary;

char gender[5];

public:

void getInput();

void putOutput();

};

Member Variables

Member functions

(can be defined inside the class or outside class)

5 of 13

Object

  • Instance of a class
  • After object creation(instantiation), memory will be allocated.
  • Syntax:

  • Example:

  • Accessing Class Members:

Class_name Object_Name;

Person p1;

Object_name.member;

p1.getInput();

6 of 13

A Complete Example

class Person{

private:

string name;

int age;

public:

void getInput();

void putOutput()

};

void Person::getInput(){

cin>>name;

cin>>age;

}

void Person::putOutput(){

cout<<name;

cout<<age;

}

int main(){

Person p1;

p1.getInput();

p1.putOutput();

return 0;

}

7 of 13

Static Members

  • Static Member Variable
    • Used to share same copy of data amongst all the objects.
    • Syntax: static data_type identifier
    • Example: static int count;
    • Defining member: data_type class_name:: variable_name;
  • Static Member Function:
    • Access to only static members declared in the same class
    • defined using static keyword
    • Can be called using class name. ( class_name :: function_name )

8 of 13

Constructor

  • Special member function which initializes objects.
  • Its name is the same as the class name.
  • Invokes automatically at the time of object creation.
  • Should be declared in the public section
  • Do no have return types
  • Cannot be inherited
  • Makes implicit call the new and delete operators

9 of 13

Default Constructor

  • Accepts no parameters
  • If not defined compiler by default provides it.
  • Syntax:

class class_Name{

member variable;

public:

class_name(){ //default constructor

//member variable initialization

}

};

10 of 13

Parameterized Constructor

  • Take arguments
  • Used to initialize every object with different set of values.
  • Syntax:

  • How to Call:
    • class_name object_name(para1,para2);
    • class_name object_name = class_name(para1,para2);

class class_Name{

member variable;

public:

class_name(para1,para2){ //parameterized constructor

//member variable initialization

}

};

11 of 13

Copy Constructor

  • initializes an object using another object of the same class.
  • Syntax:

class class_Name{

member_variable;

public:

class_name(const class_name &old_obj){ //parameterized constructor

member_variable=old_obj.member_variable;

}

};

class_name new_obj = old_obj; //invoking copy constructor

12 of 13

Destructors

  • member function which destructs or deletes an object.
  • called automatically when the object goes out of scope.
  • have same name as the class preceded by a tilde (~).
  • Does not have return type.
  • Syntax:

~class_name();

13 of 13

Inline Function

  • Function with inline keyword
  • compiler places a copy of the code of that function at each point where the function is called at compile time.
  • Syntax:

inline return_type class_name :: function_name(){��}