0% found this document useful (0 votes)
8 views11 pages

Unit2 pART1

The document provides an overview of Objects and Classes in C++, emphasizing their role in Object-Oriented Programming (OOP). It covers the definition and importance of classes and objects, access modifiers, member functions, and constructors, including their types and overloading. Key concepts such as data members, public/private/protected access, and the creation of objects are also discussed.

Uploaded by

sabho2325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Unit2 pART1

The document provides an overview of Objects and Classes in C++, emphasizing their role in Object-Oriented Programming (OOP). It covers the definition and importance of classes and objects, access modifiers, member functions, and constructors, including their types and overloading. Key concepts such as data members, public/private/protected access, and the creation of objects are also discussed.

Uploaded by

sabho2325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit -2

1.Objects and Classes


 In C++, classes and objects are the building blocks of Object-Oriented Programming (OOP).
 A class is a user-defined data type that serves as a blueprint for crea ng objects. It defines the
structure by specifying a ributes (called data members) and behaviors (called member func ons).
 An object is a specific instance of a class that occupies memory and can be used to store real-world
data.
 Objects are important because they represent en es such as a Student, Car, Employee, etc., in
programming. The main idea behind classes and objects is to bring real-world modeling into so ware
systems.
 When we declare a class, no memory is allocated for its
members un l we create objects.
 For example, a class Student may have data members like
name, age, and rollNumber, along with func ons like
display() or calculateGrade(). By crea ng mul ple objects
from the same class, we can handle mul ple students
simultaneously.

General Syntax of a Class in C++

Explana on:
 class → keyword to define a class.
 ClassName → name of the class (should start with uppercase by conven on).
 access_specifier → defines access level (public, private, or protected).
 data members → variables to hold object data.
 member func ons → func ons to operate on data members.
General Syntax for Crea ng an Object
 ClassName objectName;
 ClassName → the name of the class.
 objectName → the variable (object) created of that class.
You can also create mul ple objects of the same class in one line:
ClassName obj1, obj2, obj3;

Example 1: Basic Class and Object

Example 2: Mul ple Objects


2. Empty Classes
An empty class is a class that has no data members and no member func ons. In C++, such a class may seem
useless at first, but it has its importance.

Even though an empty class contains nothing, the C++ compiler s ll assigns 1 byte of memory to it. This is
done so that each object of the class has a unique memory address, which allows us to differen ate between
two objects.

3. Data Members and Accessing Them


 Data members are the variables declared inside a class.
 They represent the a ributes or proper es of the class.
 For example, in a Employee class, data members can be name and id.
 These data members can be accessed using the dot (.) operator through an object of the class.
 Data members can be declared with different access modifiers (public, private, protected).

4.Access Modifiers in C++
Access modifiers in C++ are keywords that define the visibility and accessibility of class members (data
members and member func ons). They control how the members of a class can be accessed from inside and
outside the class.
C++ provides three types of access modifiers:
1. Public
2. Private
3. Protected

1. Public Access Modifier


 Members declared as public are accessible from anywhere in the program using the object of the class.
 Public members are not restricted and can be directly used.

Example: Public Access

Explana on:
Here, name, rollNo, and display() are public, so they can be directly accessed using the object s1.

2. Protected Access Modifier

 Members declared as protected behave like private members but with one difference:

o They cannot be accessed directly outside the class.

o However, they can be accessed in derived (child) classes.

 Protected is mainly used in inheritance.


3.Private Access Modifier

 Members declared as private are accessible only within the class.


 They cannot be accessed directly from outside the class.
 Usually, data members are kept private to achieve data hiding, and we use ge er and se er func ons
to access them indirectly.
Example: Private Access

Explana on:
Here, balance is private, so it cannot be accessed directly. Instead, we use the public se er and ge er
func ons to access it. This ensures data hiding.

5.Member Func ons and Their Types


Member func ons define the behavior of a class. These are func ons wri en inside a class to operate on its
data members. There are mainly two types:

1. Inside Class Defini on – Func ons defined directly inside the class are inline by default.
2. Outside Class Defini on – Func ons declared inside the class and defined outside using the scope
resolu on operator (::).
Diagram: Func on Loca on
Class
├── Data Members
├── Member Func on (inside class)
└── Member Func on (outside class with ::)
Example 1: Func on Inside and Outside Class

Explana on:-
 Inside the class:
o setDimensions() and display() are defined directly inside the class.
 Outside the class:
o area() is only declared inside the class.
o Later, it is defined outside using Rectangle::area().
o The :: operator (scope resolu on operator) tells the
compiler that this func on belongs to the Rectangle
class.
6. Constructor
 A constructor is a special member func on of a class that is automa cally called when an object of that
class is created. It is mainly used to ini alize data members of the class.
 The name of the constructor is always the same as the class name.
 Constructors do not have a return type, not even void.
 They can be parameterized (accept arguments) or default (without arguments).

 Parameterized Constructor:
When you create objects s1("Ayushi", 21) and s2("Harsh", 22), the constructor automa cally runs and assigns
values to name and age.
 Automatic Execution:
The message "Constructor called!" shows that the constructor is invoked as soon as an object is created,
without needing a separate function call.
 Display Function:
The display() function prints the stored details (name and age) for each object.
1. Types of Constructors in C++

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor (optional, for heap allocation — can be taught later)
5. Constructor Overloading (multiple constructors in one class)

a.Default Constructor

A constructor with no parameters. It is automatically invoked when an object is created.


Syntax

Explanation
 When Student s1; is created, the default constructor initializes id = 0 and name = "Unknown".
 No values are passed during object creation.

b..Parameterized Constructor

A constructor that takes parameters to ini alize the object with user-defined values.
Explana on
 Here, when Student s1(101, "Harsh"); is created, the
constructor assigns values directly.
 It gives flexibility to ini alize objects with different
values.

c.Copy Constructor in C++


A copy constructor is a special type of constructor that creates a new object as a copy of an exis ng object. It
takes a reference of another object of the same class as its argument and duplicates its data members.
 Its purpose is to ini alize an object by
copying values from another object.
 The syntax always includes a parameter of
type reference to the same class
(ClassName &obj).
 If we do not define a copy constructor, the
compiler automa cally provides a default
copy constructor which performs a
shallow copy (bitwise copy of data
members).
Explana on
 s1 is created with the parameterized constructor.
 s2 = s1; invokes the copy constructor, which copies values of s1
into s2.
 Useful in func on arguments, returning objects from func ons,
and cloning objects
7.Constructor Overloading in C++
Constructor Overloading means having mul ple constructors in the same class with different parameter lists.
Depending on the number and type of arguments passed while crea ng an object, the appropriate constructor
is executed.
 Like func on overloading, constructors are overloaded to provide mul ple ways of ini alizing an
object.
 It improves flexibility by allowing objects to be created in different forms.
 Example

Explana on
 Student s1; calls the default constructor.
 Student s2(101); calls the 1-parameter constructor.
 Student s3(102, "Priya"); calls the 2-parameter constructor.
 Compiler decides which constructor to call by matching arguments.

You might also like