From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Objects and Classes in C++

Author: Ashwini Patil

Keywords: Classes, Objects, Video tutorial.

Visual Cue
Slide 1 Welcome to the spoken tutorial on Classes and Objects in C++.
Slide 2

In this tutorial we will learn,



Encapsulation. and

Data abstraction.

We will do this with the help of an example.

Slide 3

To record this tutorial, I am using

Ubuntu OS version 11.10

g++ compiler version 4.6.1

Slide 4 Let us start with the introduction to classes.

Class is created using a keyword class

It holds data and functions.

Class links the code and data.

The data and functions of the class are called as members of the class.

Slide 5 Let us move on to objects.

Objects are variables.

They are the copy of a class.

Each of them has properties and behavior.

Properties are defined through data elements. And

Behavior is defined through member functions called methods.

Slide 6 Now let us see the syntax for a class

class class-name



(public member functions)


Here, class is a keyword used to define a class.

Class-name is the name of the class.

Public, private and protected are the access specifier.

And here we have defined the Data members and the member functions

As public, private and protected.

This is how we close the class.

Open the file class-obj on gedit. Now let us see an example

I have already typed the code on the editor.

I will open it.

Point the cursor


Note that our filename is name class-obj.cpp

In this example we will calculate the area of a square using class.

Let me explain the code now.



This is our header file as iostream.

using namespace std;

Here we are using the std namespace


class square

This is declaration for a class named square.

int x;

Here I have not declared any access specifier. So by default it is private. Hence variable x is a private member of class square.



This is the public specifier

Function area is a public function.

And this is how we close the class.

Now let us move back to our slides to know more about the access specifiers.

Slide 7 Public specifier

The public specifier allows the data to be accessed outside the class.

A public member can be used anywhere in the program.

Slide 8 Private specifier

The members declared as private cannot be used or accessed outside the class.

Private members can be used only by the members of the class.

Slide 9 Protected specifier

Protected members cannot be accessed from outside the class.

They can be accessed by a derived class.

Let us move back to our program.

int square :: area (int a) Here in this statement we have the class name

The scope resolution operator and the function name.

We must use this operator.

It specifies that function area is not a global function.

It is a member function of class square.

(int a) Here we have passed an argument as int a.

Now let us switch back to the slides to know more about the scope resolution operator.

Slide 10

It is used to access the hidden data.

To access the variable or function with the same name we use the scope resolution operator ::.

Suppose the local variable and the global variable have same name.

The local variable gets the priority.

We can access the global variable using scope resolution :: operator.

Now switch to our program.
x=a; Here the value of a is stored in x



return (x*x);


Then we return the area of the square

Here x is a private member.

To access the private parameter we used the public member a.

private members are always hidden.


int main()

This is our main function.

square sqr;

Here, sqr is the object of class square.

This is how we create an object

class-name followed by the object-name


cout <<"Area of the square is " <<sqr.area() << “\n”;

Here we call the function area using the object sqr and a (dot). operator.

Then we pass an argument as 4.

we set the value of x as 4.


return 0;

This is our return statement
Click on Save Now Click on Save
Let us execute the program.
Open the terminal window

Ctrl, Alt and Tkeys simultaneously

Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.

g++ class-obj.cpp -o class

To execute



To compile type

g++ space class-obj.cpp space -o space class

Press Enter



Press Enter



The output is displayed as:

Area of the square: 16

On the text editor. Now let us move back to our program.


class square


int x;


int area(int);


What does Encapsulation mean.

What is Data abstraction..

So far now we have seen,

The data and functions combined together in a class.

Class is a single unit.

In which the data and the function using them is grouped.

This mechanism is called as Encapsulation.

Then we have seen class with the private and public members.

The private data is hidden.

It cannot be accessed outside the class.

This mechanism is called as Data abstraction.

The interface is seen but the implementation is hidden.

This brings us to the end of this tutorial.

Let us move back to our slides.

Slide 7


Let us summarize

In this tutorial we have learnt,


Data Abstraction

Private members

int x;

Public functions

int area(int);


class square

To create object-name

square sqr;

To call a function using object

Slide 6


As an assignment

write a program to find the perimeter of a given circle.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown below

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to,

Slide Number 10


Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at the link shown below:\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors