Advanced-C++/C2/Classes-And-Objects/English

From Script | Spoken-Tutorial
Revision as of 12:43, 5 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Objects and Classes in C++

Author: Ashwini Patil

Keywords: Classes, Objects, Video tutorial.


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


In this tutorial we will learn,

Classes.

Objects.

Encapsulation.

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 v. 4.6.1

Slide 4 Let us start with the introduction to classes.

Class is created using a keyword class

Class 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.

Behavior is defined through member functions called methods.

Slide 6 Now let us see the syntax for a class

Class is defined as follows

class class-name

{

public:

(public member functions)

};

Here, class keyword is 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

class-obj.cpp

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.

Highlight

#include<iostream>

This is our header file as iostream.
Highlight

using namespace std;

Here we are using std namespace



Highlight

class square


This is declaration for a class named square.
Highlight

int x;

Here the variables x is declared as private members.

By default the access specifier is private.

Hence variable x is a private member of class square.

This is the public specifier.

Highlight

public

Function area is a public function.

This is how e close the class.

Now let us move back to lides to know more about 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 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 :: operator.

Suppose the local variable and global variable have same name.

The local variable gets the priority.

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

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



Highlight

{

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.

Highlight

int main()

This is our main function.
Highlight

square sqr;

Here, sqr is the object of class square.

This is how we create an object

class-name followed by the object-name

Highlight

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

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

Then we pass an argument as 4.

we set the value of x as 4.

Highlight

return 0;

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

Ctrl, Alt and Tkeys simultaneously

Open the terminal by pressing Ctrl, Alt and T keys simultaneously
Type

g++ class-obj.cpp -o class

To execute

Type

./class

To compile the program type

g++ class-obj.cpp -o class

Press Enter

To execute type

./class

Press Enter

Highlight

Output

The output is displayed as:

Area of the square: 16

On the text editor. Let us move back to our program.



Highlight

class square

{

int x;

public:

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 private and public members.

The private data is hidden.

It members 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 slides.

Slide 7

Summary

In this tutorial we have learnt,

Encapsulation

Data Abstraction

Private members

eg. int x;

Public functions

eg. void values(int);

Classes

eg. class square

To create object-name

eg. square sqr;

To call a function using object

eg. sqr.area();

Slide 6

Assignment

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

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,

contact@spoken-tutorial.org

Slide Number 10


Acknowledgement

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: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini