0% found this document useful (0 votes)
11 views12 pages

OOP Concepts and C++ Basics for Class VIII

Uploaded by

kakolimarik
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)
11 views12 pages

OOP Concepts and C++ Basics for Class VIII

Uploaded by

kakolimarik
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

Delhi Public School Ruby Park, Kolkata

Class – VIII

Subject: Computer Science

Extra Study Material

 OOPs Concept:

To create an application using C++, it is important to understand the concepts of Object Oriented Programming (OOPs). To
understand the concept of an object, let us consider the example of building a conference hall for a school. To build the
conference hall, we start by building a room with defined boundaries and features, such as the dimensions and colours of
walls. Next, we put together other components, such as chairs, tables, telephones, projection screens and books, in the hall.
These components, such as room, chairs and tables, which we use to build the conference hall, are the objects of a real-life
scenario and are similar to the objects in the Object Oriented Programming.

Object Oriented Programming is a programming environment, where an application system is considered as a collection
of objects.

The objects interact with each other to complete a task. Object Oriented Programming simplifies the process of designing,
programming and program debugging. In C++, everything in the world is seen as an object.

 Introduction to Class:

Let us discuss the various concepts of Object Oriented Programming. Consider an analogy. A vehicle has various
properties like colour, model, gears etc. It also has certain behaviors like start, stop, speed etc. You can differentiate one
vehicle from another with the help of these properties.

Since different vehicles such as cars, bikes and trucks have these properties and behaviors, they can be defined under the
vehicles group.

In Object Oriented Programming, a class is the blueprint from which different objects are created.

Thus, vehicle is a class creating various objects like cars, bikes and trucks. The members of a class cannot be used without
its object(s).
 Syntax:

The syntax used for defining a class is:

class class-name

data members;

member functions();

};

You can define a class as a template that has certain properties (variables) and behaviours (functions). Objects
customise these properties and behaviors.

 Introducing objects:

From the analogy discussed, you can understand that cars, bikes, trucks etc. are defined as objects of a class vehicle. Every
object in OOPs has specific properties (variables) and behaviors (functions). These properties and behaviors are defined in
the class and are given specific values using objects.

Thus, objects are defined as an instance of a class.


Syntax for creating an object of a class is:

Class-name object-name;

For example, the syntax to create an object car of a class vehicle is:

Vehicle Car;

 Abstraction:

Again, consider an analogy. While watching television, you change channels, adjust the volume, adjust brightness etc.
However, you do not know the internal details, such as how the signals work or how the images are displayed on the
screen. So, you know the details that are important for you and the lack of knowledge regarding the internal details does
not create difficulty in watching television.

Similarly, a C++ class gives the required information (variables and functions) to the other parts of the program and hides
the details related to the functions. This is called data abstraction.
Thus, data abstraction is the process of providing only significant information to the outside members and hiding
the background details.

Data abstraction helps to protect the class internals from user-level errors. Moreover, if the class needs to be modified at a
later stage, it does not affect the user-level interaction with the class.

 Encapsulation:

Consider a capsule you take to keep an illness at bay. Within the shell, the capsule has powder. However, you only see the
capsule shell. You do not see the powder within it.

Similarly, in OOPs, encapsulation means data hiding. It hides the properties and functions into a single unit named as a
class so that data is protected from the outside world.

Encapsulation bundles the data and functions that make use of them and keeps them safe from outside intervention and
misuse. Thus, encapsulation is the process of enclosing the data and functions in the same place.

 Inheritance:

Consider an analogy. A child has his own traits. He also inherits the traits of his parents. Similarly, the parents inherit the
traits or behaviors of their parents, that is, the grandparents of the child. Here, 'Grandparents' is considered a base class for
the derived class 'Parents', which, in turn, acts as the base class for the derived class 'Child'.

In this way, the features of an existing class can be inherited by a new class.
Inheritance is the process of creating a new class based on an existing class. This is why the existing class is known as
the base class. Inheritance allows an object of a class to obtain the properties of the objects of another class. Thus,
inheritance allows reuse of the code.

The new class, i.e., derived class has the combined features of both the base and derived classes.

'Grandparents' is called the base class or the parent class of the class 'Parents'.

'Parents' is called the derived class or the child class of the class 'Grandparents'.

'Parents' is called the base class or the parent class for the class 'Child'.

'Child' is called the derived or child class for the class 'Parents'.
 Polymorphism:

As the name suggests, 'poly' means 'many' and 'morph' means 'having more than one forms'.

You know that people speak different languages, such as English, Bengali and Telugu.

Consider speak() as the function or behavior of the Human Beings class. Some people use this function to speak English,
some to speak Bengali, and so on. They can speak their respective languages using speak().

Thus, in OOPs, when the same function displays different behaviors in different instances, it is called polymorphism.
The behavior changes according to the type of data used and the requirement.

Introduction to C++ :
The C++ language is considered a convenient language for beginners. It is the general purpose programming language,
comprising the features of Object Oriented Programming. In 1980's, Bjarne Stroustrup of Bell Laboratories created a
programming language called C++. C++ was based on C programming language.

He added features of Object Oriented Programming (OOPs) to C without making any change in the C component. Thus,
C++ can be called a superset of C. The C++ language has various versions, such as Turbo C++, Borland C++, Visual C++
and Code Warrior (Mac). You can use any version to generate C++ programs.

 Executing a C++ Program:

To execute a C++ program, you need the following sets of software:


Editor: An editor is used to write and modify a program. It is also known as source code.
Compiler: A compiler is used to convert the source code into machine instructions, which are then executed by the
computer.

Linking program: A linking program is used to link the components of a compiled program with another to generate the
complete machine-executable program.

Debugger: A debugger assists in diagnosing errors when compiling programs. It is also used if the object program provides
unplanned results.

 Program Creation & Execution in Turbo C++:

The Turbo C++ editor is a compiler and a built-in Integrated Development Environment (IDE). The Turbo C++ editor
allows you to write, modify, compile and execute the programs. It also provides colour coding, which helps you to easily
understand the structure of programs. Add a comment in the beginning of a program to specify the program functioning.
For this, type the code, /* Writing first program in C++ */.

A comment is an explanatory statement that allows you to understand the code easily. Comments do not appear in the output
screen. C++ supports two types of comments: single-line and multi-line comments.
A single-line comment starts with // and the remaining line is considered to be a comment.

A multi-line comment starts with /* and ends with */.

 Adding header files:

Add the header files. These files are needed to display the message on the screen and take the input from the user.

A C++ program begins with a header file called pre-processor. The lines are examined before beginning the program
compilation.

#include<iostream.h> performs the standard input and output operations on the screen.

#include<conio.h> includes functions for the console input or output.

 Declaring the main() function within the class:

Add a main function. This is where the program execution starts. After adding this, the basic structure of a program gets
completed. To do this, type the code:

#include<iostream.h>

#include<conio.h>

void main()

}
The main function is the main structure of the C++ program in which some statements are processed. This function is called
when you execute the program. In the main function, ‘void’ signifies that the function does not return any value. The
parentheses ‘()’ after 'main' specify the information you need to pass to the function. Here, you need not pass any
information. Therefore, it is blank.

 Adding the cout statement to the display message:

Add the cout statement in the main function to display the message. To do this, type the code as shown. cout is an output
object used to print to the screen.

It is used together with the insertion operator ( << ). The insertion operator writes the data on the screen.

cin is an input object used to accept the user input. It is used together with the extraction operator ( >> ).

At the end of the message, Welcome to C++, you can see a special character '\n'.

'\n' is a new line character used to insert a new line.

 Holding the output screen using the getch() function:

Write getch(); statement to hold the output screen.


 Saving the Program:
1. Click File --> Save to save your program.
2. The Save File As window appears.
3. In C++, you save your program with .CPP as the extension.
4. Write the file name, [Link], in the box and click OK to save the file.

 Write the First Program:

/* Writing first program in C++ */


#include<iostream.h>
#include<conio.h>
void main()
{
cout<<" Welcome to C++ \n";
getch();
}

 Compilation:
Compilation is the process of converting the source code into the object code and linking these object codes to form the
executable code. Execution is the process of running the executable code. To do this, click Compile.

If your program does not have any error, it will show success on compilation, as shown on the screen. Execute the program.
To do this, click Run.

 Output screen:
After you run the program, the output screen appears.

To compile and execute the program, you can also press Alt+F9 and Ctrl+F9 , respectively.

You might also like