Difference between revisions of "Advanced-C++/C2/Classes-And-Objects/English"
(Created page with ''''Title of script''': Objects and Classes in C++ '''Author: '''Ashwini Patil '''Keywords: Classes, Objects, Video tutorial.''' {| style="border-spacing:0;" | style="backgro…') |
|||
Line 26: | Line 26: | ||
'''Objects.''' | '''Objects.''' | ||
− | '''Encapsulation.''' | + | '''Encapsulation.''' and |
'''Data abstraction.''' | '''Data abstraction.''' | ||
Line 41: | Line 41: | ||
'''Ubuntu OS '''version 11.10 | '''Ubuntu OS '''version 11.10 | ||
− | '''g++ compiler ''' | + | '''g++ compiler '''version 4.6.1 |
|- | |- | ||
Line 49: | Line 49: | ||
'''Class is created using a keyword class''' | '''Class is created using a keyword class''' | ||
− | ''' | + | '''It '''holds''' data '''and''' functions.''' |
Class links the '''code''' and '''data'''. | Class links the '''code''' and '''data'''. | ||
Line 66: | Line 66: | ||
'''Properties''' are defined through '''data''' elements. | '''Properties''' are defined through '''data''' elements. | ||
+ | And | ||
Behavior is defined through member functions called '''methods'''. | Behavior is defined through member functions called '''methods'''. | ||
Line 73: | Line 74: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us see the syntax for a class | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us see the syntax for a class | ||
− | |||
'''class class-name''' | '''class class-name''' | ||
Line 85: | Line 85: | ||
}; | }; | ||
− | Here, '''class''' | + | Here, '''class''' is a keyword used to define a class. |
'''Class-name''' is the name of the class. | '''Class-name''' is the name of the class. | ||
Line 125: | Line 125: | ||
'''using namespace std;''' | '''using namespace std;''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using std namespace | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using the '''std namespace''' |
− | |- | + | |-'''Class''' is defined as follows |
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight | ||
Line 143: | Line 143: | ||
'''int x;''' | '''int x;''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | + | ||
− | + | ||
+ | Here I have not declared any '''access specifier'''. | ||
+ | So by default it is private. | ||
Hence variable '''x''' is a '''private''' member of''' class square.''' | Hence variable '''x''' is a '''private''' member of''' class square.''' | ||
− | |||
− | |||
|- | |- | ||
Line 155: | Line 153: | ||
'''public''' | '''public''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Function | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| 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 | + | Now let us move back to our slides to know more about the access specifiers. |
|- | |- | ||
Line 173: | Line 172: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Private specifier''' | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Private specifier''' | ||
− | The members declared as '''private''' cannot be accessed outside the '''class'''. | + | 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'''. | '''Private''' members can be used only by the members of the '''class'''. | ||
Line 195: | Line 194: | ||
We must use this operator. | We must use this operator. | ||
− | It specifies that function '''area''' is not a global function . | + | It specifies that function '''area''' is not a global function. |
It is a member function of class square. | It is a member function of class square. | ||
|- | |- | ||
− | | style="background-color:#ffffff;border-top: | + | | style="background-color:#ffffff;border-top:0relational-operators-english pravin1389@gmail.com.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''(int a)''' |
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have passed an argument as int a. | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have passed an argument as int a. | ||
Line 212: | Line 211: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| It is used to access the hidden '''data.''' | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| It is used to access the hidden '''data.''' | ||
− | To access the variable or function with the same name we use '''::''' | + | To access the variable or function with the same name we use the scope resolution operator '''::'''. |
− | Suppose the '''local''' variable and '''global''' variable have same name. | + | Suppose the '''local''' variable and the '''global''' variable have same name. |
The '''local''' variable gets the priority. | The '''local''' variable gets the priority. | ||
− | We can access the '''global''' variable using | + | We can access the '''global''' variable using scope resolution '''::''' operator. |
|- | |- | ||
Line 227: | Line 226: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''x=a;''' | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''x=a;''' | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the value of a is stored in x | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the value of a is stored in x | ||
− | |||
− | |||
Line 267: | Line 264: | ||
'''<nowiki>cout <<"Area of the square is " <<sqr.area() << “\n”;</nowiki>''' | '''<nowiki>cout <<"Area of the square is " <<sqr.area() << “\n”;</nowiki>''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we call the function '''area '''using the''' object sqr '''and a '''. operator'''. | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we call the function '''area '''using the''' object sqr '''and a (dot)'''. operator'''. |
Then we pass an argument as 4. | Then we pass an argument as 4. | ||
Line 277: | Line 274: | ||
'''return 0;''' | '''return 0;''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our return statement |
|- | |- | ||
Line 288: | Line 285: | ||
|- | |- | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal window |
'''Ctrl, Alt and T'''keys simultaneously | '''Ctrl, Alt and T'''keys simultaneously | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard. |
|- | |- | ||
Line 303: | Line 300: | ||
'''./class''' | '''./class''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile type |
− | '''g++ class-obj.cpp -o class''' | + | '''g++ space class-obj.cpp space -o space class''' |
Press '''Enter''' | Press '''Enter''' | ||
− | + | Type | |
'''./class''' | '''./class''' | ||
Line 325: | Line 322: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the text editor. | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the text editor. | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|Now let us move back to our program. |
− | + | ||
− | + | ||
Line 360: | Line 355: | ||
This mechanism is called as''' Encapsulation.''' | This mechanism is called as''' Encapsulation.''' | ||
− | Then we have seen class with''' private '''and''' public '''members'''.''' | + | Then we have seen class with the ''' private '''and''' public '''members'''.''' |
The''' private '''data is hidden. | The''' private '''data is hidden. | ||
− | '''It''' | + | '''It''' cannot be accessed outside the '''class.''' |
This mechanism is called as '''Data abstraction.''' | This mechanism is called as '''Data abstraction.''' | ||
Line 374: | Line 369: | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. | ||
− | Let us move back to slides. | + | Let us move back to our slides. |
|- | |- | ||
Line 380: | Line 375: | ||
Summary | Summary | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we have learnt, | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize |
+ | |||
+ | In this tutorial we have learnt, | ||
Encapsulation | Encapsulation | ||
Line 388: | Line 385: | ||
'''Private''' members | '''Private''' members | ||
− | + | int x; | |
'''Public''' functions | '''Public''' functions | ||
− | + | int area(int); | |
'''Classes''' | '''Classes''' | ||
− | + | class square | |
To create object-name | To create object-name | ||
− | + | '''square sqr;''' | |
To call a function using '''object''' | To call a function using '''object''' | ||
− | + | '''sqr.area();''' | |
|- | |- | ||
Line 418: | Line 415: | ||
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Watch the video available at the link shown | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Watch the video available at the link shown below |
It summarizes the Spoken Tutorial project | It summarizes the Spoken Tutorial project | ||
Line 447: | Line 444: | ||
It is supported by the National Mission on Education through ICT, MHRD, Government of India | 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 http://spoken-tutorial.org\NMEICT-Intro] | + | More information on this Mission is available at the link shown below: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro] |
|- | |- |
Latest revision as of 12:01, 3 February 2014
Title of script: Objects and Classes in C++
Author: Ashwini Patil
Keywords: Classes, Objects, Video tutorial.
|
|
Slide 1 | Welcome to the spoken tutorial on Classes and Objects in C++. |
Slide 2
|
In this tutorial we will learn,
Classes. Objects. 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
{ public: (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
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 the std namespace
|
Highlight
class square
|
This is declaration for a class named square. |
Highlight
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. |
Highlight
public |
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
|
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 (dot). operator.
Then we pass an argument as 4. we set the value of x as 4. |
Highlight
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. |
Type
g++ class-obj.cpp -o class To execute Type ./class |
To compile type
g++ space class-obj.cpp space -o space class Press Enter Type ./class Press Enter |
Highlight
Output |
The output is displayed as:
Area of the square: 16 |
On the text editor. | Now let us move back to our program.
|
Highlight
class square { int x; public: int area(int); };
|
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
Summary |
Let us summarize
In this tutorial we have learnt, Encapsulation Data Abstraction Private members int x; Public functions int area(int); Classes class square To create object-name square sqr; To call a function using object 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 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, contact@spoken-tutorial.org |
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: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank You for joining. |