Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Oo ps exam answer2
1. 1. Difference between C and C++
C++ is an extension of C language. This means that you can not only use the new features introduced with C++ but can also use the power
and efficiency of C language. C and C++ are no more language for writing compilers and other languages, these general purpose languages
are used worldwide in every field.
The main difference between C and C++ is that C++ is object oriented while C is function or procedure oriented. Object oriented
programming paradigm is focused on writing programs that are more readable and maintainable. It also helps the reuse of code by
packaging a group of similar objects or using the concept of component programming model. It helps thinking in a logical way by using the
concept of real world concepts of objects, inheritance and polymorphism. It should be noted that there are also some drawbacks of such
features. For example using polymorphism in a program can slow down the performance of that program.
On the other hand, functional and procedural programming focus primarily on the actions and events, and the programming model focuses
on the logical assertions that trigger execution of program code.
2. What is polymorphism? Explain with example
Polymorphism refers to the ability to assume different forms. In OOP, it indicates a language’s ability to handle objects differently based on
their runtime type. When objects communicate with one another, we say that they send and receive messages.
An example of polymorphism can be demonstrated with geometric shapes. Suppose we have a Triangle, a Square, and a Circle. Each class is
a Shape and each has a method named Draw that is responsible for rendering the Shape to the screen. With polymorphism, you can write a
method that takes a Shape object or an array of Shape objects as a parameter (as opposed to a specific kind of Shape). We can pass
Triangles, Circles, and Squares to these methods without any problems, because referring to a class through its parent is perfectly legal. In
this instance, the receiver is only aware that it is getting a Shape that has a method named Draw, but it is ignorant of the specific kind of
Shape. If the Shape were a Triangle, then Triangle’s version of Draw would be called. If it were a Square, then Square’s version would be
called, and so on.
Advantage of polymorphism is that the sender of a message doesn’t need to know which class the receiver is a member of. It can be any
arbitrary class. The sending object only needs to be aware that the receiving object can perform a particular behaviour.
3. What are the basic data types?
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data
processing is accomplished by executing series of commands called program. A program usually contains different types of data types
(integer, float, character etc.) to store the values being used in the program along with some library function and user defined function
(UDF) to process that stored data. C language is rich of data types and library function. A C programmer has to employ proper data type as
per his /her requirements has different data types for different types of data and can be broadly classified as:
Primary data types
Secondary data types
4. Explain briefly control statements with an example.
When a programmer is crafting a program, it is good practice to break the program down into pieces that can be thought of independently.
Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain
sequence. These pieces then pass the control of the program between each other. While one piece has the control, the other pieces are
inactive. This is known as the flow of control in a program. If our program had three parts, called Start, Middle, and End, the flow of control
could look like:
2. Control statements: Control Statements, then, are ways for a programmer to control what pieces of the program are to be executed at
certain times. The syntax of Control statements are very similar to regular English, and are very similar to choices that we make every day.
There are two basic types of control statements: branching statements and loops.
Branching statements
We will first look at branching statements. Let's say Julien is shopping at a mall and he finds a CD that he wants to buy. Julien then checks
his pocket to see if he has enough money to pay for the CD. When he pulls his money out of his pocket Julian may be thinking: "if I have
more money than the price of the CD then I will buy the CD." In pseudocode that thought could be translated into:
if (my_money > cost_of_CD) then
buy_CD
else
get_a_job
end if;
Loops
Let's pretend now that Julien was buying a house instead of a CD. If Julien wanted to buy the house without taking a loan from the bank, he
would have to wait until he had enough money to buy the house. The pseudocode for this could be:
if (my_money > cost_of_house) then
buy_house
end if;
But this means that Julien would only check once if he had enough money to buy the house. What we want to describe is the fact that Julien
needs to keep waiting until he has enough money to buy the house.
while (my_money < cost_of_house)
work_more
end while;
buy_house;
This is a loop statement.
5. Explain briefly about the escape sequence characters? Give example?
A character constant in C++ must contain one or more characters and must be enclosed in single quotation marks. For example ‘A’, ‘9’, etc.
C++ allows nongraphic characters which cannot be typed directly from keyboard, e.g., backspace, tab, carriage return etc. These characters
can be represented by using a escape sequence. An escape sequence represents a single character. The following table gives a listing of
common escape sequences.
Escape Sequence Nongraphic Character
a Bell (beep)
b Backspace
f Formfeed
n Newline or line feed
r Carriage return
t Horizontal tab
v Vertical tab
? Question mark
Backslash
’ Single quote
” Double quote
xhh Hexadecimal number (hh represents the number in hexadecimal)
000 Octal number (00 represents the number in octal)
0 Null
6.Explain class with an example?
An object, such as a CD Player, a printer, a car, etc, is built from assembling various parts. In the same way, C++ allows you to group
various variables and create a new object called a class. A class is an organisation of data and functions which operate on them. Data
structures are called data members and the functions are called member functions, The combination of data members and member functions
constitute a data object or simply an object.
Imagine a company that manufactures shoe boxes hires you to write a program that would help design and identify those shoe boxes. A shoe
box is recognized for its dimensions (length, width, height), color, and shoe size that a particular box can contain, etc. The variables that
characterize such an object could be:
double Length, Width, Height;
char Color;
float ShoeSize;
A Class Example
class ShoeBox
{
double Length, Width, Height;
char Color[12];
float ShoeSize;
};The items that compose a class are called members of the class.
7. Features of c++ language.
The C++ programming language is based on the C language.
In C++, you can develop new data types that contain functional descriptions (member functions) as well as data representations.
These new data types are called classes.
The work of developing such classes is known as data abstraction.
You can work with a combination of classes from established class libraries, develop your own classes, or derive new classes
from existing classes by adding data descriptions and functions.
New classes can contain (inherit) properties from one or more classes.
3. The classes describe the data types and functions available, but they can hide (encapsulate) the implementation details from the
client programs.
You can define a series of functions with different argument types that all use the same function name.
This is called function overloading. A function can have the same name and argument types in base and derived classes.
Declaring a class member function in a base class allows you to override its implementation in a derived class. If you use virtual
functions, class-dependent behaviour may be determined at run time. This ability to select functions at run time, depending on
data types, is called polymorphism
Adding properties to operators for new data types is called operator overloading.
The C++ language provides templates and several keywords not found in the C language.
Other features include try-catch-throw exception handling, stricter type checking and more versatile access to data and functions
compared to the C language.