Class in C++
- A class in c++ has a class name, a set of attributes (data members/characteristics) and a set of actions or services(function members/member functions).
- A class represents a set of objects that share a common structure and a common behavior.
- The class specifies the type and scope of variables and functions declared inside the class. The variables(data) declared inside the class are known as data members/members data and the functions(methods) are known as member functions.
- Being the part of the class, data and function are called members of the class. The body of the class is enclosed within braces and terminated by the colon. The keywords private, public and protected are known as visibility labels, which defines the visibility of members.It is common practice to declare data members as private and member functions as public.
- The declaration of a class does not define any object but only specify the structure of objects i.e. what they will contain.
- A class must be instantiated (object creation) in order to make use of the services provided by it.This process of creating objects (variables) of the class is called class instantiation or instantiating of objects.
Object in C++
- An object may have a name, a set of attributes or member data, and a set of actions or services or methods.
- An object is an instance of a class.
- In Object-Oriented domain an object is one of the many things that together constitute the problem domain.
- An object may stand alone or it may belong to a class of similar objects.Examples of objects that may stand alone are knife, frame … etc. Examples of objects that belong to a class of similar objects are: your car, your table … etc.
- Objects are the basic run-time entities in Object-Oriented programming language. They occupy space in memory that keeps its state and is operated on by the defined operations/functions on the object, while a class defines a possible set of objects.
- According to Webster‟s New World Dictionary object is defined as “A thing that can be seen or touched; material thing; a person or thing to which action, thought or feeling is directed”.
- Syntax : –
- Example –
- Here, the declaration of an object is similar to that of any basic type and the keyword ‘class’ is optional.
- Objects can also be created by placing their names immediately after the closing brace,similar as ‘structure’ in C. Thus
- An object of a class is able to accesses the members of a class .We can access the member of a class using the member assess operator, dot(.). The syntax for assessing data member of a class is : – objectName.dataMember; where as the syntax for assessing member functions of a class is : – objectName.functionName(actualArguments, if any);
Member/Data Member(Member Data) & Member Function of a Class
- Member of a class is used data variables and functions.
- The data member of a class is variables that must be declared within the body of the class either publically/privately/protected.
- A member function is a function which is always declared inside a class and is responsible for performing certain operation required by the class.
- It is the actual interface to initiate an action of an object belonging to a class.
- It is normally used to read, manipulate, or display the data member.
- The member functions of a class can be defined in two way : – (i) Function is defined inside the class (ii) Function is defined outside the class with the help of scope resolution operator(::).However, irrespective of the location of their definition, the member function must have identical code and perform the same operation. The compiler treats these two definitions in a different manner.
Nesting of Member Functions
- When a member function call any another member function of the same class directly, inside it, irrespective of its privilege, without using the dot operator. This is called as nesting of member functions.
- In other words, A member function can be called by using its name inside another member function of the same class is known as nesting of member functions.
- In nested member function, a function that includes another function inside it is called outer function and the function included inside outer function is called inner function. Here, outer function is called with the help of object of that class first and then inner function called directly with the help of outer function.
- Reason to choose Inline Function – Normally, a member function call, transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function save program space and memory space and are used because the function is stored only in one place and is only executed when it is called. This execution may be time consuming since the registers and other processes must be saved before the function gets called. The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function.
- Inline functions can execute much faster than normal functions.
- The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.
- An inline function is written in one line when they are invoked. These functions are very short, and contain one or two statements.
- Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.
- If inline function is too large, then program grows larger (become slower). So, in general, only short functions are declared as inline functions.
- To declare a function inline, simply precede the function’s definition with the inline specifier.
- Generally a member function is made inline by adding a keyword inline in front of its return type in its declaration.
- The general format of inline function is as follows:
- By doing inline, the function gets executed as inline, which removes the control transfer to that function’s body.
- Inline function instruct compiler to insert complete body of the function wherever that function got used in code.
- inline functions might decrease the number of cache misses thus Inline function usually improves CPU-bound application to run faster.
- They behave like normal functions, so any return value would be reflected to the caller just like a normal function.
- The functions are defined as inline when the function is small and is called many times.
- Using this, function call overhead doesn’t occur.
- It also saves the overhead of push/pop variables on the stack when function is called.
- It also saves overhead of a return call from a function.
- As we know that, the private members cannot be accessed from outside the class.
- Also, a typical non-member function (a normal function outside the class)cannot have an access right to the private data of a class but C++ provides a special feature called the friend function (a non-member function, i.e., not a member of any class.) which is used to access the private members of a class.
Some special properties of friend function are : –
- A friend function is not in the scope of the class to which it has been declared as friend.
- A friend function cannot be called using the object of that class. It is called simply as function.
- It can be invoked like a normal function without the use of any object.
- Unlike member functions of a class, it can not access the class members directly. However, it can use the object and dot operator with each member name to access both private and public members of a class.
- Friend function can be declared either in the public or the private part of a class without affecting its meaning.
- Usually, it has got objects as arguments.
- Like friend function, a class can also be a friend of another class.
- A friend class is a class that can access all the private and protected members of other class in which it is declared as friend.
- In order to access the private and protected members of a class into friend class we must pass on object of a class to the member functions of friend class.
- This is needed when we want to allow a particular class to access the private and protected members of a class.
- A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.
Scope Resolution Operator(::)
- Scope resolution operators are specific operators that work on names rather than values.
- The main use of scope resolution operators are in various important activities in C++. These are –
- If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable.
- This operator is used to get hidden names of variable due to variable scopes, so that we can still use them.
- It is also used to define a function outside the class.
- It is used to access the static variables of class.
- It is used to referring a class inside another class.
- In case of multiple Inheritance.
- In Namespace declaration.
- It can be used as both unary and binary operation.
Visibility/Accessibility/Access Specifiers of class members
- During class declaration, we have normally used three types of visibility – private, public and protected.They are called visibility labels used to control the access to members (data members and member functions) of a class.It is necessary for data encapsulation, is to prevent accidental modification of information of a class and also data can be protected from external tempering by users and access to specific methods can also be controlled.
- To access the members of a class in controlled way (so that a process is managed well), a set of rules is imposed in which a class is to be manipulated and data and functions of the class can be accessed. The set of rules is known as accessibility rules. This accessibility rules are achieved by using these three keywords – private, public and protected. These keywords are also called access-control specifiers.
- In C++, Members of a class (member data & member function), without any access specifier is considered as private by default.
- A class which is totally private (both member data & member function), is hidden from the external world and usually not serve any useful purpose/not behavioral.
- Normally, private members of a class are not accessible by outside class but we can access the private member of a class from outside a class using friends, pointers to members etc. from outside the class.
- A class can use all the three visibility/accessibility labels.
- The Access Specifiers table is –
Access specifiers Accessible to Same class members Objects of same class public Yes Yes protected Yes No private Yes No
- The public members of a class can be accessed directly by objects of same or another class (having no security) without member function of the class.
- The private members of the class can only accessed by public member function of the same class.
- The protected members of a class is similar as private except, the object of derived class can access protected members of base class directly without member function of the base class.
- The protected access specifier is mainly associated with Inheritance.
- Function overloading is one of the most powerful features of C++ programming language.
- It forms the basis of polymorphism (compile-time polymorphism).
- Function Overloading not only does it provide support for compile time polymorphism, it also adds flexibility and convenience in the program.
- Function overloading refers to using the same thing for different purposes.
- C++ permits the use of different functions with the same name. having essentially different argument list.The difference can be in terms of number of arguments or datatype of arguments or both.It is only through these differences, the compiler knows which function to call in at any given situations.
- The compiler know when to call which function, if there are more than one function of the same name.The function call determines which function definition will be executed.
- In another words, the process of using two or more functions with the same name but differing in the signature is called function overloading
- These functions can perform a variety of different tasks i.e. the biggest advantage of overloading is that it helps us to perform same operations on different datatypes without having the need to use separate names for each version.
- Overloading of functions with different return types are not allowed.
- The use of overloading may not have reduced the code complexity /size but has definitely made it easier to understand and avoided the necessity of remembering different names for each version function which perform identically the same task.
- When an overloaded function is called, C++ goes through the following process to determine which version of the function will be called, i.e. first, C++ tries to find an exact match in which the actual argument exactly matches the parameter type of one of the overloaded functions.
- The working mechanism of function overloading can be explained using three outcomes –
- If a match is found, the call is resolved to a particular overloaded function.
- If no match is found, the arguments can not be matched to any overloaded function.
- If an ambiguous match is found, the arguments matched more than one overloaded
- Ambiguous matches is a situation in which the compiler is unable to choose between two (or more) overloaded functions.When this happens, the situation is said to be ambiguous. Ambiguous statements are errors, and programs containing ambiguity will not compile.Thus, ambiguous matches are considered a compile-time error.
- An ambiguous match needs to be disambiguated before the program will compile.There are two ways to resolve ambiguous matches:
- The best way is simply to define a new overloaded function that takes parameters of exactly the type we are trying to call the function with. Then we will be able to find an exact match for the function call.
- Alternatively, explicitly cast the ambiguous parameter(s) to the type of the function we want to call.
Static Keyword (Static Data Member & Static Member Function)
- Static is a keyword or a storage class specifier/qualifier that provides information about locality and visibility of variables in a class.
- The members of a class can be made static using this keyword before the name of data member and function member of a class.
- Static data members are accessible by all objects of that class equally and each object uses the same static data member.
- A class may contain both static as well as non-static data members, with or without static member function.
- Static data member is must initialized with zero/other values when the first object of its class is created. They are normally initialized outside the class.
- static data member is a variable and is part of a class ,not the part of an object of that class, hence is called class variable.
- There is exactly one copy of static data member instead of one copy per object, as for ordinary non-static data members.
- The static member function can only access the static member(data or function) declared in the same class.
- A static member function is called using the class name directly instead of its object.
Passing Objects (as Arguments) to Function, Returning Objects & Objects Assignment in a Class
- When we pass objects as arguments/parameters to a function, normally inside main method, like any other data type of a class is known as passing objects as arguments.
- This can be done by a pass-by-value and a pass-by-reference. In pass-by-value, a copy of the object is passed to the function and any modifications made to the object inside the function are not reflected in the object used to call the function. While, in pass-by-reference, an address of the object is passed to the function and any changes made to the object inside the function is reflected in the actual object.
- we can also return object from the function like any other data type.
798 total views, 5 views today