Topics |
---|
1. Class
Class is a collection of objects sharing common properties and behaviour.
Or
Class is Simply a Collection of similar objects.
2. Need for class
To define real world objects more effectively. (i.e. not only data associated with them but also their associated behaviour or operations.)
3. Defining classes : Declaration of Class
1. Data Members
2. Member Functions
3. Program Access Levels
4. Class Tagname
Syntax
class < Class Name >
{
private :
[Var Declaration]
[Function Declarations]
protected :
[Var Declaration]
[Function Declarations]
public:
[Var Declaration]
[Function Declarations]
} ;
Example :
class A
{ int a;
public:
void getdata()
{
cin>>a;
}
void showdata()
{
cout <
}
};
4. Access Specifiers
Access specifiers are used to identify access rights for the data and member functions of the class. There are main types of access specifiers in C++ programming language :
♦ Private
♦ Public
♦ Protected
Private
A private member within a class denotes that only members of the same class have accessibility. The private member is not accessible from outside the class. (Note : Default access specifier, if no access specifier is provided then all the Data members and member functions are treated as private members).
Public
Public means are accessible from outside the class, using objects of class.
Protected
A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.
While defining access specifiers, the programmer must use the keywords : private, public or protected when needed, followed by a colon and then define the data and member functions under it.
5. Defining Function Outside the Class (out of scope of class)
Member Functions of a class can be defined outside the class. For this purpose the scope resolution operator ( : : ) can be used.
Return_type class_name : : function_name (argument list)
{
// statements to be executed
}
Example :
class A
{
int a;
public :
void getdata ();
void showdata ();
} ;
void A : : getdata ()
{
cin >>a;
}
void A : : showdata ()
{
cout <
}
The private members provide data hiding by preventing us from accessing the data directly.
The class declaration must end with a semicolon.
It is important to include the corresponding header file otherwise, we will get compile time errors. In the # include, the name of the file is enclosed in quotes, not in angle brackets. Angle brackets are used for including, standard library header files, and quotes are used for including our own header files(this tells the compiler where to look for the header file).
6. Creation of Objects
Once the class created, one or more objects can be created from the class as objects are instance of the class. Just as we declare a variable of data type int as : int x;
Objects are also declared as :
class name followed by object name;
ex e1;
This declares e1 to be an object of class ex.
For example, a complete class and object declaration is given below:
class ex
{
private:
int x, y;
public:
void sum()
{
............
............
}
} ;
main ()
{ ex e1;
............
.............
}
The object can also be declared immediately after the class definition. In other words, the object name can also be placed immediately after the closing flower brace symbol '}' of the class declaration.
Example :
class ex
{
private :
int x, y;
public:
void sum ()
{
..............
..............
}
}e1;
7. Inline Functions
Advantage
The inline functions are a C++ enhancement designed to speed -up the programs. They save the overhead of function call, as the function is not invoked, rather its code is replaced in the program.
Disadvantage
With more function calls, more memory is wasted as for every function call, the same function code is inserted in the program. Repeated occurrences of same function code waste memory space.
8. Differentiate between protected and private members.
The protected and private members are hidden from the outside world of a class i.e.,both cannot be accessed by non-member and non-friend functions. However, the difference is that private members are not inherited by the derived class whereas protected members are inherited by the derived class.
9. What do you understand by a class in C++?
A class is an entity that defines a group of similar objects. A class is a way to bind the data describing an entity and its associated functions together. In C++, a class makes a datatype that is used to create objects of this type.
10. What do you understand by nested class?
When a class definition contains definition of another class, this is known as nesting. The outer class is known as enclosing class and inner class is known as nested class.
11. What is friend function?
A friend function is a non-member function that's grated access to a class's private and protected members.
12. What is access specifier?
Access specifier is way to specify the scope and accessibility of members of a class. Access specifier can be either private or protected or public.
13. What is static data member?
A static data member is created for the class rather than objects. Only one copy of a static data member exists and all objects of the class share this copy.
14. Differentiate between Data Hiding and Encapsulation.
Data hiding is a property that enforces that only relevant information is exposed to the user and rest of the information remains hidden from the user.
Encapsulation is a way to implement data hiding by wrapping up data and associated function into a single unit, class. The groups its members (data & functions) into three sections : private, protected and public where private and protected members remain hidden from outside world and thereby support data hiding.
15. What is the significant of scope resolution operator : : ?
The : : operator uncovers a hidden global item. For example, in the following code fragment,
int X :
class A { public :
int X ;
void f (int i)
{ X = i ; // assign i to A : : X (local X)
: : X = i ; // assign i to : : X (global X)
} ;
: : X refers to the global X whereas only X refers to the local X i.e., A : : X (X member of class A).
16. How is a member function of a class defined or declared?
The member functions of a class can be define in two places:
(i) Outside the class definition (ii) Inside the class definition.
When a member function is defined outside the class definition, the name of the function must be the full name (i.e., qualified name) of the function i.e.,
class_name : : function_name
Inside the class, the member function definition is similar to an ordinary function. Also, a member function defined internally automatically becomes inline.
17. How is working of a member function different from a friend function and a non member function?
1. Member and friend functions have full access privilege to both the public and private members of the class while, in general, nonmember function have access only to the public members of the class.
2. Member functions are defined within the class scope that is they are not visible outside the scope of class. Friend and non member functions are also visible outside the scope of class.
18. What do you understand about a member function? How does a member function differ from an ordinary function?
Member function have full access privilege to all the members (public, protected and private members) of the class while, in general, nonmember function have access only to the public member of the class.
Member functions are defined within the class scope that is they are not visible outside the scope of class. Whereas nonmember functions are also visible outside the scope of class.
19. In C++, classes can be created using class keyword and struct keyword. Comment.
Yes, classes can be created using class or struct keywords. The classes created through struct keywords are classes with a little difference. All members are public by default in classes created through struct in contrast to classes created through class keyword where members are private by default.
20. What is the relationship of a class and its objects? How is memory allocated to a class and its objects?
A class represents a group of similar objects. For instance, 'bird' is a class and 'crow' is an object. An object is an instance of the class. In C++, class makes a data type using which its objects can be created.
When a class is defined, memory is allocated for its member functions and they are stored in the memory. When an object is created, separate memory space is allocated for its data members. All objects works with the one copy of member functions shared by all.
21. Describe the methods of accessing data members and member functions of a class in the following cases:
(a) Inside the main program. (b) Inside a member function of the same class.
(c) Inside a member function of another class
(a) Inside the main program. If the main program has access privilege for that particular class say X, then the public members are accessed using the syntax
object_name.public member_name
where object-name if the name of the object of type X.
(b) Inside the member function of the same class. Here all the members of the class are accessed as ordinary members. No class name or object name is required here.
(c) Inside the member function of another class. Here only the public members are accessed using the objects of the same type (i.e., X type)
i.e., object name. member name.
The private and protected members are not accessible directly
The members of a class can be accessed by another class only if the other class has access privilege for that class.
22. While defining a class, you can define its methods (member functions) inside or outside the class definition. How are these two definitions different?
There are basically two different in these two definitions:
1. In the way the function names are written. When a member function is defined outside the class definition, its name must be a qualified name i.e., its name must be in the form classname : : function name whereas this is not required for an inside definition.
2. In the way the functions are processed. A member function defined inside the class definition are automatically treated as inline functions which is not the case when the functions are defined outside the class definition unless the functions are explicitly made inline.
23. What is the significants of access labels in a class?
A class provides three access labels namely, private, protected, and public.
A member declared as private or protected remains hidden from outside world and it can only be accessed by the member functions and friend functions of the class.
A member declared as public is made available to the outside world. That is, it can be accessed by any function, any expression in the program but only by using an object of the same class type.
These access labels enforce data-hiding and abstraction, OOP concepts.