Unit 2
Unit 2
SUBJECT CODE:
SYLLABUS
UNIT II
Classes and Objects: Declaring Objects – Defining Member Functions –Static Member variables
and functions – array of objects –friend functions – Overloading member functions – Bit fields
and classes – Constructor and destructor with static members.
CLASS:
A Class is a user-defined data type that has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables together, these data members and member functions define the
properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage, etc, and
member functions can be applying brakes, increasing speed, etc.
OBJECT
Declaring Objects
When a class is defined, only the specification for the object is defined; no memory or storage
is allocated. To use the data and access functions defined in the class, you need to create
objects.
Syntax
Accessing data members and member functions: The data members and member functions
of the class can be accessed using the dot(‘.’) operator with the object. For example, if the
name of the object is obj and you want to access the member function with the name print
Name() then you will have to write obj. print Name().
Accessing Data Members
The public data members are also accessed in the same way given however the private data
members are not allowed to be accessed directly by the object. Accessing a data member
depends solely on the access control of that data member. This access control is given
by Access modifiers in C++. There are three access modifiers: public, private, and protected.
C++ program to demonstrate accessing of data members
#include <bits/ std c++.h>
using namespace std;
class Geeks {
// Access specifier
public:
// Data Members
string geek name;
// Member Functions()
void printname() { cout << "Geekname is:" << geekname; }
};
int main()
{
// Declare an object of class geeks
Geeks obj1;
// accessing data member
obj1.geekname = "Abhi";
// accessing member function
obj1.printname();
return 0;
}
Member Functions in Classes
To define a member function outside the class definition we have to use the scope resolution::
operator along with the class name and function name.
// C++ program to demonstrate function
#include <bits/stdc++.h>
class Geeks
public:
string geekname;
int id;
void printname();
void printid()
};
void Geeks::printname()
int main() {
Geeks obj1;
obj1.geekname = "xyz";
obj1.id=15;
// call printname()
obj1.printname();
// call printid()
obj1.printid();
return 0;
Output
Geekname is: xyz
In C++, a member function is a function that is associated with a specific object or class.
Member functions are defined within the class definition, and they have access to the data
members and other member functions of the class. They can be used to perform operations on
the object’s data and interact with objects of the same class.
A member function can be invoked by using dot notation, which is the object’s name followed
by the name of the member function and its parameters. For example, if we have a class called
“Person” with a member function called “speak()”, we can call this function for an object of that
class called “obj1” using the notation “obj1.speak()”.
When a member function is defined as “const”, it can be called on a constant object and will not
modify the object.
One important thing to remember when working with member functions is that, by default, they
do not have access to the non-static member variables of the class unless this pointer is used to
reference them explicitly.
In the above figure, we have data members and member functions in class named student.
Data members or class members are nothing, just simple variables. And Member functions are
nothing, just simple functions we use in a program.
These member functions are defined with the “static” keyword. They do not have access to the
data members of the class and can only access static data members. They can be called without
creating an object of the class.
FRIEND FUNCTION
These member functions are used to overload operators for a class, allowing you to use operators
such as +, -, *, /, etc., with objects of that class.
They are also types of member functions; they are special member functions that are
automatically generated by the compiler when needed.
class IntegerClass {
int anInteger;
...
// define methods here
...
}
Notice that the member variable declaration appears within the body of the class
implementation but not within a method. This positioning within the class body
determines that the variable is a member variable.
Like other variables in Java, member variables must have a type. A variable's
type determines the values that can be assigned to the variable and the
operations that can be performed it. You should already be familiar with data
types in Java through your reading of
Variables and Data Types
in the previous lesson.
A member variable's name can be any legal Java identifier and by convention
begins with a lower case letter (class names typically begin with upper case
letters). You cannot declare more than one member variable with the same name
in the same class. However, a member variable and a method can have the same
name. For example, the following code is legal:
class IntegerClass
{
int anInteger;
int anInteger()
{ // a method with the same name as a member variable
ARRAY OF OBJECT
Syntax:
ClassName ObjectName[number of objects];
The Array of Objects stores objects. An array of a class type is also known as an array of
objects.
Example#1:
Storing more than one Employee data. Let’s assume there is an array of objects for storing
employee data emp[50].
Below is the C++ program for storing data of one Employee:
#include<iostream>
class Employee
{
int id;
char name[30];
public:
};
cout<<"Enter Id : ";
cin>>id;
cin>>name;
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
int main(){
return 0:
In the above example, a class named Employee with id and name is being considered.
The two functions are declared-
getdata(): Taking user input for id and name.
putdata(): Showing the data on the console screen.
This program can take the data of only one Employee. What if there is a requirement to add
data of more than one Employee. Here comes the answer Array of Objects. An array of objects
can be used if there is a need to store data of more than one employee. Below is the C++
program to implement the above approach
BIT FIELDS:
bit field by specifying the data type and then the field name with the desired width in bits.
A constructor is a special member function of a class and shares the same name as of class,
which means the constructor and class have the same name. Constructor is called by the
compiler whenever the object of the class is created, it allocates the memory to the object and
initializes class data members by default values or values passed by the user while creating an
object. Constructors don’t have any return type because their work is to just create and
initialize an object.
// private members
// constructor body
};
Default constructor
Parameterized constructor
Copy Constructor
Dynamic Constructor
Default Constructor
Syntax
class class_name{
public:
// declaring default constructor
class_name()
// constructor body
};
Parameterized Constructor
Parameterized constructor is used to initialize data members with the values provided by the
user. This constructor is basically the upgraded version of the default constructor. We can
define more than one parameterized constructor according to the need of the user, but we have
to follow the rules of the function overloading.
Dynamic Constructor
When memory is allocated dynamically to the data members at the runtime using a new
operator, the constructor is known as the dynamic constructor. This constructor is similar to the
default or parameterized constructor; the only difference is it uses a new operator to allocate
the memory.
Destructor is just the opposite function of the constructor. A destructor is called by the
compiler when the object is destroyed and its main function is to deallocate the memory of the
object. The object may be destroyed when the program ends, or local objects of the function
get out of scope when the function ends or in any other case.
Destructor has the same as of the class with prefix tilde(~) operator and it cannot be
overloaded as the constructor. Destructors take no argument and have no return type and return
value.
How Constructor and Destructor are called when the object is Created and
Destroyed
As constructor is the first function called by the compiler when an object is created and the
destructor is the last class member called by the compiler for an object. If the constructor and
destructor are not declared by the user, the compiler defines the default constructor and
destructor of a class object.
Let’s see a code to get the proper idea of how constructor and destructor are called:
First, we will create a class with single parametrized constructors and a destructor. Both of
them contain print statements to give an idea of when they are called.