Difference between revisions of "Advanced-C++/C2/Classes-And-Objects/English-timed"
From Script | Spoken-Tutorial
Line 418: | Line 418: | ||
|- | |- | ||
| 06:32 | | 06:32 | ||
− | |'''classes''''''class square''' | + | |'''classes''' '''class square''' |
|- | |- |
Latest revision as of 12:43, 23 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on Classes and Objects in C++. |
00:07 | In this tutorial, we will learn: |
00:09 | Classes |
00:11 | Objects Encapsulation and |
00:14 | Data abstraction. |
00:16 | We will do this with the help of an example. |
00:20 | To record this tutorial, I am using: |
00:23 | Ubuntu OS version 11.10 |
00:28 | g++ compiler version 4.6.1 |
00:32 | Let us start with the introduction to classes. |
00:36 | Class is created using a keyword class. |
00:39 | It holds data and functions. |
00:42 | Class links the code and data. |
00:45 | The data and functions of the class are called as members of the class. |
00:51 | Let us move on to objects. |
00:53 | Objects are variables. |
00:55 | They are the copy of a class. |
00:58 | Each of them has properties and behavior. |
01:01 | Properties are defined through data elements and |
01:06 | behavior is defined through member functions called methods. |
01:10 | Now let us see the syntax for a class. |
01:14 | Here, class is a keyword used to define a class. |
01:18 | Class-name is the name of the class. |
01:21 | public, private and protected are the access specifier. |
01:26 | And here we have defined the data members and the member functions as public, private and protected. |
01:34 | This is how we close the class. |
01:37 | Now let us see an example. |
01:39 | I have already typed the code on the editor. |
01:42 | I will open it. |
01:44 | Note that our filename is class hyphen obj dot cpp |
01:50 | In this example, we will calculate the area of a square using class. |
01:56 | Let me explain the code now. |
01:58 | This is our header file as iostream. |
02:02 | Here we are using the std namespace. |
02:06 | This is declaration for a class named square. |
02:10 | Here, I have not declared any access specifier. |
02:14 | So, by default it is private. |
02:17 | Hence, variable x is a private member of class square. |
02:22 | This is the public specifier. |
02:25 | Function area is a public function. |
02:28 | And this is how we close the class. |
02:31 | Now let us move back to our slides to know more about the access specifiers. |
02:36 | Public specifier- |
02:39 | The public specifier allows the data to be accessed outside the class. |
02:44 | A public member can be used anywhere in the program. |
02:49 | Private specifier- |
02:51 | The members declared as private cannot be used or accessed outside the class. |
02:57 | private members can be used only by the members of the class. |
03:03 | Protected specifier- |
03:05 | protected members cannot be accessed from outside the class. |
03:10 | They can be accessed by a derived class. |
03:13 | Let us move back to our program. |
03:16 | Here, in this statement we have the class name, |
03:21 | the scope resolution operator and the function name. |
03:25 | We must use this operator. |
03:27 | It specifies that function area is not a global function. |
03:33 | It is a member function of class square. |
03:36 | Here, we have passed an argument as int a. |
03:40 | Now, let us switch back to our slides to know more about the scope resolution operator. |
03:46 | It is used to access the hidden data. |
03:49 | To access the variable or function with the same name, we use the scope resolution operator ::. |
03:56 | Suppose the local variable and the global variable have same name. |
04:01 | The local variable gets the priority. |
04:05 | We can access the global variable using ::(scope resolution operator.) |
04:10 | Now, switch to our program. |
04:12 | Here the value of a is stored in x. |
04:17 | Then we return the area of the square. |
04:20 | Here x is a private member. |
04:22 | To access the private parameter, we used the public member a. |
04:27 | private members are always hidden. |
04:30 | This is our main function. |
04:33 | Here, sqr is the object of class square. |
04:37 | This is how we create an object. |
04:40 | class-name followed by the object-name. |
04:43 | Here, we call the function area using the object sqr and a .(dot) operator. |
04:50 | Then we pass an argument as 4. |
04:53 | We set the value of x as 4. |
04:57 | This is our return statement.Now click on Save. |
05:00 | Let us execute the program. |
05:03 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
05:11 | To compile, type: g++ space class hyphen obj dot cpp space hyphen o space class |
05:20 | Press Enter. |
05:22 | Type./class(dot slash class). |
05:24 | Press Enter.The output is displayed as: |
05:28 | Area of the square is 16 |
05:30 | Now, let us move back to our program. |
05:35 | So far now we have seen, |
05:37 | the data and functions combined together in a class. |
05:41 | class is a single unit |
05:44 | in which the data and the function using them is grouped. |
05:49 | This mechanism is called as Encapsulation. |
05:53 | Then we have seen class with the private and public members. |
05:59 | The private data is hidden. |
06:02 | It cannot be accessed outside the class. |
06:05 | This mechanism is called as Data abstraction. |
06:09 | The interface is seen but the implementation is hidden. |
06:14 | This brings us to the end of this tutorial. |
06:17 | Let us move back to our slides. Let us summarize. |
06:20 | In this tutorial, we have learnt: |
06:23 | Encapsulation Data Abstraction |
06:25 | Private members |
06:27 | int x; |
06:29 | Public functions int area(int); |
06:32 | classes class square |
06:35 | To create object, |
06:37 | square sqr; |
06:39 | To call a function using object sqr dot area(); |
06:43 | As an assignment: write a program to find the perimeter of a given circle. |
06:49 | Watch the video available at the link shown below. |
06:52 | It summarizes the Spoken Tutorial project. |
06:55 | If you do not have good bandwidth, you can download and watch it. |
07:00 | The Spoken Tutorial Project team: |
07:02 | Conducts workshops using spoken tutorials. |
07:05 | Gives certificates to those who pass an online test. |
07:09 | For more details, please write to: contact@spoken-tutorial.org |
07:16 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
07:20 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
07:26 | More information on this mission is available at the link shown below. |
07:31 | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |