0% found this document useful (0 votes)
3 views

cpp unit 2

The document provides an overview of classes and constructors in C++, explaining key concepts such as data members, member functions, encapsulation, access specifiers, and the roles of constructors and destructors. It also covers inheritance, polymorphism, function definitions, inline functions, and scope rules, along with the use of static members and friend functions/classes. The document emphasizes the importance of these concepts in implementing object-oriented programming principles.

Uploaded by

kunalkumbhar0985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cpp unit 2

The document provides an overview of classes and constructors in C++, explaining key concepts such as data members, member functions, encapsulation, access specifiers, and the roles of constructors and destructors. It also covers inheritance, polymorphism, function definitions, inline functions, and scope rules, along with the use of static members and friend functions/classes. The document emphasizes the importance of these concepts in implementing object-oriented programming principles.

Uploaded by

kunalkumbhar0985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT 2

OBJECT, CLASSES AND CONSTRUCTOR


CLASS:
In C++, a class is a user-defined data type that encapsulates data
and functions into a single unit, referred to as an object. It serves as a
blueprint for creating objects with specific attributes and behaviors.
Classes allow for the implementation of object-oriented programming
(OOP) concepts such as encapsulation, inheritance, and polymorphism.
1. Data Members: These are variables declared within the class,
representing the attributes or properties of objects created from that
class. Data members can be private, protected, or public, determining
their accessibility from outside the class.

2. Member Functions (Methods): Member functions are functions


defined within the class to operate on its data members. These functions
define the behavior or actions that objects of the class can perform. Like
data members, member functions can also be private, protected, or
public.

3. Encapsulation: Classes in C++ provide encapsulation by bundling


data and functions together within a single unit (class). Encapsulation
hides the internal state of objects and restricts direct access to data
members from outside the class. This helps in achieving data
abstraction and information hiding, enhancing the security and
maintainability of code.

4. Access Specifiers: C++ provides three access specifiers: `public`,


`private`, and `protected`. These specifiers control the accessibility of
class members.
- `public`: Members declared as public are accessible from outside
the class.
- `private`: Members declared as private are only accessible within
the class itself.
- `protected`: Members declared as protected are accessible within
the class and its derived classes.

5. Constructor and Destructor: Constructors are special member


functions used for initializing objects of the class. They have the same
name as the class and are automatically called when an object is
created. Destructors, on the other hand, are used to release resources or
perform cleanup operations when an object is destroyed.

6. Objects: Objects are instances of a class created using the class


blueprint. Each object has its own set of data members and can call
member functions defined in the class.

7. Inheritance: Inheritance is a mechanism in C++ where a class


(derived class) can inherit properties and behaviors from another class
(base class). This promotes code reuse and allows for the creation of a
hierarchy of classes.

8. Polymorphism: Polymorphism allows objects of different classes to


be treated as objects of a common base class. It enables functions to
behave differently based on the object they are operating on, facilitating
dynamic binding and runtime polymorphism through virtual functions.

SYNTAX :
SYNTAX USING FUNCTION OUTSIDE THE
CLASS :

FUNCTION:
The definition of a function in programming refers to providing the
actual implementation or code body of a function. It specifies what the
function does when it is called. In languages like C++, a function
declaration merely declares the function's name, return type, and
parameters without providing its actual implementation. The definition,
on the other hand, includes the complete function body.
TYPES OF THE FUNCTIONS
Without argument without return value

With argument without return value

With argument with return value

Without argument with return value


Calling a Function
Calling a function in C++ involves executing a block of code that is
defined elsewhere in the program. To call a function:
1. Identify the function you want to call.
2. Provide any required arguments if the function expects them.
3. Use the function's name followed by parentheses () to invoke it.
When the function is called, the program jumps to the function's
definition, executes the code within the function, and then returns
control to the point in the program immediately after the function call.
Inline Function
Inline functions in C++ are small functions that are expanded in place
at each point they are called, rather than being executed through a
function call. The inline keyword is used to declare a function as inline,
suggesting to the compiler that it should replace function calls with the
actual code of the function body wherever the function is called.
Definition:
An inline function is declared with the inline keyword.
It's typically defined in a header file, although it can also be defined in
the same source file where it's used.
Expanding Code:
When an inline function is called, the compiler replaces the function
call with the actual code of the function body.
This reduces the overhead of function calls, especially for small
functions, as it avoids the overhead associated with the function call
stack and return address.
Performance:
Inline functions are beneficial for performance-critical code where the
overhead of function calls needs to be minimized.
They are particularly useful for small, frequently-called functions.
Use Cases:
Inline functions are commonly used for getter and setter methods, small
utility functions, and functions that perform simple calculations.
They are not suitable for large or complex functions, as inlining large
functions can lead to code bloat and decrease in performance due to
increased memory usage.
Syntax:

Scope Rules of Functions and Variables


In C++, scope rules dictate where in a program a function or variable
can be accessed or referenced. Understanding scope is crucial for
writing clear, maintainable, and error-free code. Here are some key
points regarding the scope rules of functions and variables in C++:
1. Global Scope:
 Variables and functions declared outside of any function,
typically at the top of a file, have global scope.
 Global variables and functions can be accessed from any part of
the program, including other files (if they are declared as extern).
2. Local Scope:
 Variables declared within a function have local scope.
 Local variables are accessible only within the function in which
they are declared.
 Once the function execution completes, local variables are
destroyed, and their memory is released.
3. Function Scope:
 Functions have their own scope, and variables declared within a
function are limited to that scope.
 Function names can be re-used in different scopes, but they must
have distinct signatures (overloading).
 Nested functions are not allowed in C++, so functions cannot be
defined within other functions.
4. Block Scope:
 Blocks are segments of code enclosed within curly braces {}.
 Variables declared within a block have block scope, and they are
accessible only within that block.
 Blocks can be nested, and inner blocks can access variables
declared in outer blocks, but not vice versa.
Member Function Definition –
Inside class and outside the class using scope Resolution Operator
In C++, member functions of a class can be defined both inside the
class definition and outside the class definition using the scope
resolution operator (::). Here's how each method works:
Inside Class Definition:
When you define a member function inside the class definition, it is
implicitly considered as an inline function. The syntax for defining a
member function inside a class is similar to defining a regular function:

In this method, the member function is defined directly within the


class body. This is suitable for small functions or functions that you
want to inline for performance reasons. It's a concise way to declare
and define member functions.
Outside the Class Definition:
You can also define member functions outside the class definition
using the scope resolution operator (::) to specify that they belong to a
particular class. This is useful for separating the declaration and
definition of member functions, especially for larger functions.

In this method, the member function is declared inside the class


definition, and its definition is provided separately outside the class
definition. The scope resolution operator (::) is used to specify that the
member function belongs to the class MyClass.
Accessing Members from Object

Static Data Members:


Static data members are variables that belong to the class rather than
to individual objects. They are declared with the static keyword
within the class definition.

In this example, staticVariable is a static data member of the class


MyClass. It is initialized and defined outside the class, and it can be
accessed using the scope resolution operator (::) without creating an
object of the class.
Static Member Functions:
Static member functions are functions that belong to the class rather
than to individual objects. They are declared with the static keyword
within the class definition.

In this example, staticFunction is a static member function of the class


MyClass. It can be called using the scope resolution operator (::)
without creating an object of the class.
Static class members are useful for maintaining data or functionality
that is shared among all instances of the class, such as counting the
number of instances, providing utility functions, or managing global
settings. They can be accessed directly through the class name
without needing to create objects of the class
Friend Function:
A friend function is a function that is not a member of a class but has
access to the private and protected members of that class when
explicitly declared as a friend inside the class definition.
Friend Class:
A friend class is a class that is granted access to the private and
protected members of another class when declared as a friend inside
the class definition.

Constructor Declaration and Definition:


Declaration:
 Constructors have the same name as the class and do not have a
return type, not even void.
 They may have parameters, which can be used to initialize
member variables.

Definition:
Constructors are defined like regular functions, but they are preceded
by the class name and use the scope resolution operator (::).
The body of the constructor contains initialization code.
Declaration:
 Destructors have the same name as the class, preceded by a tilde
(~), and they also have no return type.
 Destructors cannot have parameters.

Definition:
 Destructors are defined similarly to constructors but with the
tilde (~) preceding the class name.
 The destructor typically contains cleanup code, such as releasing
resources acquired by the class.
 Destructors do not take parameters, and there can only be one
destructor per class.
 Destructors are called automatically when objects are destroyed,
either when they go out of scope (for automatic variables) or
when delete is called on dynamically allocated objects.

You might also like