C++ Classes and Data Abstraction

Estudies4you
Q1. Define a class.
Answer:

Class
A class is an abstract data type that groups the data and its associated functions. It can also hides the data and functions if required.
A class specification consists of two parts,
(i) Declaration of a class and class members
(ii) Definitions of member functions of a class.
The members scope and types are described by the class declaration and the implementation of member functions are described by the class function definitions.
Syntax
class classname
{
private:
//Variables and function declarations
public: :
//Variables and function declarations
}
A class is defined using the key word 'class' followed by name of the class. The class keyword indicates an abstract data type called name of the class. Body of class,is defined inside the curly brackets and is terminated by semicolon at the end. Body of the class includes the variables and function declarations.

Q2. Write a short note on object.
Answer:

An object can be defined as an instance of a class which is used to access the members of a class.
Declaration of Objects to a Class
The declaration of objects to a class is similar with the declaration of variables to datatypes. The method of defining the declared class objects is known as class instantiations. For every object, a separate memory is allocated. Memory allocation is done only for the member functions of each class object, An object can be declared in two methods. They are as follows,
Method 1:
An object can be declared as a normal variable.
Syntax
Classname objectname;
Example
Student stul;
Method 2
The object can also be declared as a pointer variable.
Syntax
classname *objectname;
Example
Student *stu2;

Q3. Write a short note on scope of class.
Answer:

Class scope defines the accessibility or visibility of class variables or functions. The term scope is defined as a place where in variable name, function name and typedef are used inside a program.
The different types of scopes are as follows,
  1. Local scope
  2. Function scope
  3. File scope
  4. Class scope and
  5. Prototype scope
To Top