Detailed Print – CSKC
Sign In

Search

siteicon
Solved Problem#CPP#5179 Back

Problem Statement - Code error in class

Find error (if any) in the following class definition.

Class PLAY
{
  int number_of_actors=5;
  void set_actor_count()
  {
    number_of_actors=10;
  }
};

Solution

Click to open popup

Solved Problem Understanding

//Error 1
Class PLAY
Here it should be
class PLAY  //class is a keyword and all letters should be lowercase only

//Error 2
int number_of_actors=5;
It should be
int number_of_actors; //one can not give initial values to a data members in a class, as class is just template, it doesn’t have memory space of its own. It gets memory space only when an object is declared based on class. Therefore values can be assigned in class methods only as they will be called only when object is created.