Professional Documents
Culture Documents
This statement assigns to variable x the value contained in variable y. The value of x at
the moment this statement is executed is lost and replaced by the value of y.
Consider also that we are only assigning the value of y to x at the moment of the
assignment operation. Therefore, if y changes at a later moment, it will not affect the new
value taken by x.
For example, let's have a look at the following code - I have included the evolution of the
content stored in the variables as comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// assignment operator
#include <iostream>
using namespace std;
int main ()
{
int a, b;
// a:?, b:?
a = 10;
// a:10, b:?
b = 4;
// a:10, b:4
a = b;
// a:4, b:4
b = 7;
// a:4, b:7
cout << "a:";
cout << a;
cout << " b:";
cout << b;
}
This program prints on screen the final values of a and b (4 and 7, respectively). Notice
how a was not affected by the final modification of b, even though we declared a = b
earlier.
Assignment operations are expressions that can be evaluated. That means that the
assignment itself has a value, and -for fundamental types- this value is the one assigned in
the operation. For example:
y = 2 + (x = 5);
In this expression, y is assigned the result of adding 2 and the value of another
assignment expression (which has itself a value of 5). It is roughly equivalent to:
1 x = 5;
2 y = 2 + x;
equivalent to...
y = y + x;
x = x - 5;
x = x / y;
price = price * (units+1);
and the same for all other compound assignment operators. For example:
1
2
3
4
5
6
7
8
9
10
11
Example 2
x = 3;
x = 3;
y = ++x;
y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3
In Example 1, the value assigned to y is the value of x after being increased. While in
Example 2, it is the value x had before being increased.
Relational and comparison operators ( ==, !=, >, <, >=, <= )
Two expressions can be compared using relational and equality operators. For example,
to know if two values are equal or if one is greater than the other.
The result of such an operation is either true or false (i.e., a Boolean value).
The relational operators in C++ are:
operator
description
==
Equal to
!=
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
Here there are some examples:
1
2
3
4
5
(7 == 5) // evaluates to false
(5 > 4)
// evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5)
// evaluates to false
Of course, it's not just numeric constants that can be compared, but just any value,
including, of course, variables. Suppose that a=2, b=3 and c=6, then:
1
2
3
4
Be careful! The assignment operator (operator =, with one equal sign) is not the same as
the equality comparison operator (operator ==, with two equal signs); the first one (=)
assigns the value on the right-hand to the variable on its left, while the other (==)
compares whether the values on both sides of the operator are equal. Therefore, in the last
expression ((b=2) == a), we first assigned the value 2 to b and then we compared it to a
(that also stores the value 2), yielding true.
The logical operators && and || are used when evaluating two expressions to obtain a
single relational result. The operator && corresponds to the Boolean logical operation
AND, which yields true if both its operands are true, and false otherwise. The following
panel shows the result of operator && evaluating the expression a&&b:
&& OPERATOR (and)
a
b
a && b
true true true
true false false
false true false
false false false
The operator || corresponds to the Boolean logical operation OR, which yields true if
either of its operands is true, thus being false only when both operands are false. Here are
the possible results of a||b:
|| OPERATOR (or)
a
b
a || b
true true true
true false true
false true true
false false false
For example:
1 ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false )
2 ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )
When using the logical operators, C++ only evaluates what is necessary from left to right
to come up with the combined relational result, ignoring the rest. Therefore, in the last
example ((5==5)||(3>6)), C++ evaluates first whether 5==5 is true, and if so, it never
checks whether 3>6 is true or not. This is known as short-circuit evaluation, and works
like this for these operators:
operator
&&
||
short-circuit
if the left-hand side expression is false, the combined result is false (the righthand side expression is never evaluated).
if the left-hand side expression is true, the combined result is true (the righthand side expression is never evaluated).
This is mostly important when the right-hand expression has side effects, such as altering
values:
if ( (i<10) && (++i<n) ) { /*...*/ } // note that the condition increments i
Here, the combined conditional expression would increase i by one, but only if the
condition on the left of && is true, because otherwise, the condition on the right-hand
side (++i<n) is never evaluated.
Conditional ternary operator ( ? )
The conditional operator evaluates an expression, returning one value if that expression
evaluates to true, and a different one if the expression evaluates as false. Its syntax is:
condition ? result1 : result2
If condition is true, the entire expression evaluates to result1, and otherwise to result2.
1
2
3
4
For example:
1
2
3
4
5
6
7
8
9
10
11
12
}
13
14
In this example, a was 2, and b was 7, so the expression being evaluated (a>b) was not
true, thus the first value specified after the question mark was discarded in favor of the
second value (the one after the colon) which was b (with a value of 7).
Comma operator ( , )
The comma operator (,) is used to separate two or more expressions that are included
where only one expression is expected. When the set of expressions has to be evaluated
for a value, only the right-most expression is considered.
For example, the following code:
a = (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end,
variable a would contain the value 5 while variable b would contain value 3.
Q4 ) Interactive And Passive Graphics
Interactive Graphics
In interactive graphics the user may interact with the graphics and with the programs
generating them i.e. can make any change in image show on the screen..
Passive Graphics
In passive computer graphics the user has no control over the image i.e. can't make any
change in image show on the screen.
Advantages of Interactive on Passive Graphics
Monitor
Display Controller
The image is stored in frame buffer in the form of binary numbers which represents as a
array of picture elements or pixels. For example, where you want to store only black and
white images, so you can represent black pixels by 1's and white pixels by 0's in the
frame buffer. There for a array of black and white pixels of size 16 X 16 could be
represented by 32 bytes, stored in a digital memory as shown in figure below:
After that display controller reads each byte of data from buffer and converts its 0's and
1's into the corresponding video signals. Then these signals is fed into the monitor which
produced black and white images on the screen. Display controller repeats this operation
thirty times per second to maintain steady picture on the screen. If you want to change the
image, then you need to modify the frame buffer contents to represent the new pattern of
pixels.
Q5 ) Class v/s Object in C++
In object oriented programming, a class is a construct that defines a collection of
properties and methods. It can be viewed as a template.
Objects are instances of a class. They share the common structure that the class defines.
When you define a class, you define a blueprint for a data type. This doesn't actually
define any data, but it does define what the class name means, that is, what an object of
the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class
body, enclosed by a pair of curly braces. A class definition must be followed either by a
semicolon or a list of declarations. For example, we defined the Box data type using the
keyword class as follows:
class Box{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that
follow it. A public member can be accessed from outside the class anywhere within the
scope of the class object.
A class provides the blueprints for objects, so basically an object is created from a class.
We declare objects of a class with exactly the same sort of declaration that we declare
variables of basic types. Following statements declare two objects of class Box:
Box Box1;
Box
Both of the objects Box1 and Box2 will have their own copy of data members.
Accessing the Data Members:
The public data members of objects of a class can be accessed using the direct member
access operator (.).
Eg:
Box1.length;
Q6 ) stream in C++ ( Given notes )
Q7 ) What is encapsulation
Data encapsulation is a mechanism of bundling the data, and the functions that use them
and data abstraction is a mechanism of exposing only the interfaces and hiding the
implementation details from the user. In encapsulation all the data and functions to
manipulate data are wrapped up into a single cage.
C++ supports the properties of encapsulation and data hiding through the creation of userdefined types, called classes. We already have studied that a class can contain private,
protected and public members. By default, all items defined in a class are private. For
example:
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
private:
double length;
// Length of a box
double breadth; // Breadth of a box
double height;
// Height of a box
};
The variables length, breadth, and height are private. This means that they can be
accessed only by other members of the Box class, and not by any other part of your
program. This is one way encapsulation is achieved.
To make parts of a class public (i.e., accessible to other parts of your program), you must
declare them after the public keyword. All variables or functions defined after the public
specifier are accessible by all other functions in your program.
Making one class a friend of another exposes the implementation details and reduces
encapsulation. The ideal is to keep as many of the details of each class hidden from all
other classes as possible.
Q8) features of object oriented programming (As in Q2)
Q9) Explain single and multiple inheritance (As in notes)
Q10 ) Data abstraction and encapsulation
Encapsulation is a mechanism by which you restrict the access to some of the object's
components, as well as binding the data and methods operating on the data.
Now if we consider a laptop, as an end user I have access only to some features of the
system. So I could use the mouse to move the cursor, or the keyboard for typing text, but
I would not have access to the internal components of the laptop. Again the keyboard in
turn is bound internally to a set of methods that operate in response to a user action or an
event.
Abstraction is the ability to define an object that can represent abstract entities which
can work, change state and communicate with other entities.
Let us take the example of our laptop Keyboard itself, here we have a number of Keys,
each performing some function dependent on the value given. Now all keys have a
certain value, that is accepted by the CPU when you press it. So we create a common
object called Key with following methods.
Q11 ) Differentiate inheritance and polymorphism
As in notes