Tech

Data Members, ‘set’ Functions, & ‘get’ Functions–C++ Notes (C Plus Plus Notes)

  • Variables declared in a functions body are local variables, and can be used only from the point of their declaration in the function to the immediately following closing right brace (}). When a function terminates, the values of its local variables are lost.
  • A local variable must be declared before it can be used in a function. A local variable cannot be accessed outside the function in which it’s declared.
  • Data members normally are private. Variables or functions declared private are accessible only to member functions of the class in which they’re declared, or to friends of the class.
  • When a program creates (instantiates) an object of a class, its private data members are encapsulated (hidden) in the object & can be accessed only by member functions of the object’s class.
  • When a function that specifies a return type other than VOID is called & completes its task, the function returns a result to its calling function.
  • By default, the initial value of a string is the empty string–i.e., a string that does not contain any characters. Nothing appears on the screen when an empty string is displayed!

* Remember, class variables (data members) should always be private!

* Build and use “setter” & “getter” functions to access the private variables!

‘set’ Functions & ‘get’ Functions

  • Classes often provide public member functions to allow clients of the class to ‘set’ of ‘get’ private data members. The names of these member functions normally begin with ‘set’ or ‘get’.
  • ‘Set’ and ‘get’ functions allow clients of a class to indirectly access the hidden data. The client does not know how the object performs these operations.
  • A class’s ‘set’ & ‘get’ functions should be used by other member functions of the class to manipulate the class’s private data. If the class’s date representation is changed, member functions that access the data only via the ‘set’ & ‘get’ functions will not require modification.
  • A public set function should carefully scrutinize any attempt to modify the value of a data member, in order to ensure that the new value is appropriate for that data item.

Initializing Objects with Constructors

  • Every class gets a constructer.
  • The name is always the same as the class name.
  • No return type; Not even valid is returned!
  • Each class should provide a ‘constructor’ to initialize an object of the class when the object is created. A constructor must be defined with the same name as the class.
  • A difference between constructors & functions is that constructors cannot return values, so they cannot specify a return type (not even ‘void’). Normally, constructors are declared public.
  • C++ requires a constructor call at the time each object is created, which helps ensure that every object is initialized BEFORE it’s used in a program.
  • A constructor with no parameters is a default constructor. If you do NOT provide a constructor, the compiler provides a default one. You can also define a default constructor explicitly. IF, you define a constructor for a class, C++ will not create a default constructor.
  • A constructor is a special function that gets called automatically as soon as you create an object. A constructors main reason are to give variables an initial value.
  • As soon as we create an object of a class we can have all the variables automatically “initialized/assigned” with values.
  • We can give the constructor parameters to add value(s) to the variable(s).

Notes:

Class variables (data members) should always be private. Build/use “setter” & “getter” functions to access the private variables.

Constructors

  • every class gets a constructor (a special function that gets called automatically, as soon as you create the object)
  • the constructor name is always the same as the class name
  • and without a return type (not even void)

Constructors main reason are to give variables an initial value; as soon as you create an object of a class we can have all the variables automatically “initialized/assigned” with values; give the constructor parameters to add value(s) to the variable(s).