0% found this document useful (0 votes)
17 views13 pages

OOP Classes and Objects

The document explains the concepts of classes and objects in C++ and Java, highlighting their roles in object-oriented programming. It describes how to define classes, create objects, and access data members and member functions. Examples are provided for both languages to illustrate the implementation of these concepts.
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)
17 views13 pages

OOP Classes and Objects

The document explains the concepts of classes and objects in C++ and Java, highlighting their roles in object-oriented programming. It describes how to define classes, create objects, and access data members and member functions. Examples are provided for both languages to illustrate the implementation of these concepts.
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

C++ Classes and Objects

Objects and classes are used to wrap related functions and data in one place
in C++.
Suppose we need to store the length, breadth, and height of a rectangular
room and calculate its area and volume.

To handle this task, we can create three variables, say, length , breadth ,

and height , along with the functions calculate_area() and calculate_volume() .

However, in C++, rather than creating separate variables and functions, we


can also wrap the related data and functions in a single place (by
creating objects).
This programming paradigm is known as object-oriented programming.
But before we can create objects and use them in C++, we first need to learn
about classes.

C++ Class
A class is a blueprint for the object.

We can think of a class as a sketch (prototype) of a house.

It contains all the details about the floors, doors, windows, etc - we build the
house based on these descriptions.

The house is the object.


Create a Class
A class is defined in C++ using the keyword class followed by the name of the
class.
The body of the class is defined inside curly brackets and terminated by a
semicolon at the end.

class ClassName {
// some data
// some functions
};

For example,

class Room {
public:
double length;
double breadth;
double height;

double calculate_area(){
return length * breadth;
}

double calculate_volume(){
return length * breadth * height;
}

};

Here, we defined a class named Room .

The variables length , breadth , and height declared inside the class are known
as data members.
And the functions calculate_area() and calculate_volume () are known as member
functions of a class.

C++ 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, we need to create
objects.

Syntax to Define Object in C++

ClassName object_name;

We can create objects of Room class (defined in the above example) as follows:

// sample function
void sample_function() {
// create objects
Room room1, room2;
}

int main(){
// create objects
Room room3, room4;
}
Here, two objects room1 and room2 of the Room class are created
in sample_function() .

Similarly, the objects room3 and room4 are created in main() .

As we can see, we can create objects of a class in any function of the


program.

We can also create objects of a class within the class itself or in other classes.

Also, we can create as many objects as we want from a single class.

C++ Access Data Members and Member


Functions
We can access the data members and member functions of a class by using
a . (dot) operator.
For example,

room2.calculate_area();

This will call the calculate_area() function inside the Room class for object room2 .

Similarly, the data members can be accessed as:

room1.length = 5.5;

In this case, it initializes the length variable of room1 to 5.5 .


Example: Object and Class in C++
Programming
// Program to illustrate the working of
// objects and class in C++ Programming

#include <iostream>
using namespace std;

// create a class
class Room {

public:
double length;
double breadth;
double height;

double calculate_area() {
return length * breadth;
}

double calculate_volume() {
return length * breadth * height;
}
};

int main() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculate_area() << endl;
cout << "Volume of Room = " << room1.calculate_volume() << endl;

return 0;
}
Run Code

Output

Area of Room = 1309


Volume of Room = 25132.8

In this program, we have used the Room class and its object room1 to calculate
the area and volume of a room.
In main() , we assigned the values of length , breadth , and height with the code:

room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

We then called the functions calculate_area() and calculate_volume() to perform


the necessary calculations.
Note the use of the keyword public in the program. This means the members
are public and can be accessed anywhere from the program.
Java Class and Objects
Java is an object-oriented programming language. The core concept of the
object-oriented approach is to break complex problems into smaller objects.

An object is any entity that has a state and behavior. For example,
a bicycle is an object. It has
• States: idle, first gear, etc
• Behaviors: braking, accelerating, etc.
Before we learn about objects, let's first know about classes in Java.

Java Class
A class is a blueprint for the object. Before we create an object, we first need
to define the class.

We can think of the class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows, etc. Based on these descriptions we
build the house. House is the object.

Since many houses can be made from the same description, we can create
many objects from a class.
Create a class in Java
We can create a class in Java using the class keyword. For example,

class ClassName {
// fields
// methods
}

Here, fields (variables) and methods represent the state and behavior of the
object respectively.
• fields are used to store data

• methods are used to perform some operations

For our bicycle object, we can create the class as

class Bicycle {

// state or field
private int gear = 5;

// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
}

In the above example, we have created a class named Bicycle . It contains a


field named gear and a method named braking() .

Here, Bicycle is a prototype. Now, we can create any number of bicycles using
the prototype. And, all the bicycles will share the fields and methods of the
prototype.
Note: We have used keywords private and public . These are known as access
modifiers. To learn more, visit Java access modifiers.

Java Objects
An object is called an instance of a class. For example, suppose Bicycle is a
class then MountainBicycle , SportsBicycle , TouringBicycle , etc can be considered as
objects of the class.
Creating an Object in Java
Here is how we can create an object of a class.

className object = new className();

// for Bicycle class


Bicycle sportsBicycle = new Bicycle();

Bicycle touringBicycle = new Bicycle();

We have used the new keyword along with the constructor of the class to
create an object. Constructors are similar to methods and have the same
name as the class. For example, Bicycle() is the constructor of
the Bicycle class. To learn more, visit Java Constructors.
Here, sportsBicycle and touringBicycle are the names of objects. We can use
them to access fields and methods of the class.
As you can see, we have created two objects of the class. We can create
multiple objects of a single class in Java.
Note: Fields and methods of a class are also called members of the class.

Access Members of a Class


We can use the name of objects along with the . operator to access members
of a class. For example,

class Bicycle {

// field of class
int gear = 5;

// method of class
void braking() {
...
}
}

// create object
Bicycle sportsBicycle = new Bicycle();

// access field and method


sportsBicycle.gear;
sportsBicycle.braking();

In the above example, we have created a class named Bicycle . It includes a


field named gear and a method named braking() . Notice the statement,

Bicycle sportsBicycle = new Bicycle();

Here, we have created an object of Bicycle named sportsBicycle . We then use


the object to access the field and method of the class.
• sportsBicycle.gear - access the field gear
• sportsBicycle.braking() - access the method braking()

We have mentioned the word method quite a few times. You will learn
about Java methods in detail in the next chapter.
Now that we understand what is class and object. Let's see a fully working
example.

Example: Java Class and Objects


class Lamp {

// stores the value for light


// true if light is on
// false if light is off
boolean isOn;

// method to turn on the light


void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);

// method to turnoff the light


void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
}
}

public class Main {


public static void main(String[] args) {

// create objects led and halogen


Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
led.turnOn();

// turn off the light by


// calling method turnOff()
halogen.turnOff();
}
}
Run Code

Output:

Light on? true


Light on? false

In the above program, we have created a class named Lamp . It contains a


variable: isOn and two methods: turnOn() and turnOff() .

Inside the Main class, we have created two objects: led and halogen of
the Lamp class. We then used the objects to call the methods of the class.
• led.turnOn() - It sets the isOn variable to true and prints the output.
• halogen.turnOff() - It sets the isOn variable to false and prints the
output.
The variable isOn defined inside the class is also called an instance variable. It
is because when we create an object of the class, it is called an instance of
the class. And, each instance will have its own copy of the variable.
That is, led and halogen objects will have their own copy of the isOn variable.

Example: Create objects inside the same class


Note that in the previous example, we have created objects inside another
class and accessed the members from that class.
However, we can also create objects inside the same class.

class Lamp {

// stores the value for light


// true if light is on
// false if light is off
boolean isOn;

// method to turn on the light


void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);

public static void main(String[] args) {

// create an object of Lamp


Lamp led = new Lamp();

// access method using object


led.turnOn();
}
}
Run Code

Output

Light on? true

Here, we are creating the object inside the main() method of the same class.

You might also like