Programming Withc& C++: Variables
Programming Withc& C++: Variables
PROGRAMMINGWITHC&C+t type by
new abstract data
PROGRAMMING WITHC& C++ languges a programmer
can define a
together with the
operations
s specifiers define how a member's structure,
variables and
unctions of a class can be accessed from outside specifying a data
structure.
the permissible on that data
However, all members of a class can be accessed from withinclass. the
class without any restriction. Data
|Structure
Class members can be declared as public protected or private
access specifiers, which is used to build the encapsulation
capabilities of the class. JOperations
There are three access specifiers. (functions)
1, Private members: These can be accessed only from within the
late binding.
members of the same class. Q8. Differentiate early versus
early
that occur at compile time
2, Protected members : These can be accessed only from withirn Early bindingrefers to events at compile time. Examples of
function
other members of the same class and its derived classes. binding means cell needed function calls, overloaded function
early binding incude normal
3. Public members : These can be accessed from anywhere where main advantage of early
calls, and over loaded operations. The function calls ar very fast.
the object is accessible. binding is efficiency and these type of
By declaring the member variables and functions as a private is late binding Late binding
The opposite of early bindingresolved
refers to function cells that are not until run time. Virtual
in aclass, the members are hidden from outside the class.Those the function
functions are used to achieve late binding. The object and binding is
private members and data cannot be accessed by the object directly. are not linked until run timne. The main advantage of late
Q.7. Define the tem data abstraction ? flexibility. Late binding allows you to create programs that carn
respond to events that occçr while the program executes without
is slow
Abstracton : Data abstraction is the technique of creating new having to create a large amount of. contingency code. It
data types. These new data types are suited for application compare toearly binding.
program. Data abstrection provides the ability to create user 0.9. What is the application of the scope resolution operator
defined data types, for modeling a real world object. The class in inC+t.
C++ is called abstract data types (ADT). Because A class is a
collection of user defined data types. C++ supports a Mechanism to access a global variable from a
function. It is achieved using the scope resolution operator. The
Abstraction refers to the act of representing essential features syntax is.
without including the background details. ::Global variable name
known
The data types created the data abstraction process are
powerful technique, The scope resolution operator permits a program to reference
as Abstract data types. Data abstraction is a
optimal, move readable, and an identifier in the global scope that has been hidden by another
and its proper usage will result in
suported by several other identifier with the same name in the local scope.
flexible program. Data abstraction is talk, Ada etc. In these
programming languages such as small
116 PROGRAMMING WITTH C&C+.
Abstraction refers to the act of representing essential featura.
without
them.
including the background details or explanation betweer
For example, a class Car would be made up of an Engine
Gearbox, Steering objects, and many more components. To build the
Car class, one does not need to know how the different
components
work internally, but only how to interface with them, i.e., send
messages to thenm, receive messages from them, and perhaps make
the different objects composing the class interact with each other.
(6) Messap e passing : It refers to that
communication between one place to another. establishing
(7) Encapsulation: The wrapping up of
into a single unit is called data and its functions
Encapsulation.
When using Data Encapsulation, daa is not
it is only accessible through the accessed directly,
functions present inside the class.
Data Abstraction increases the
power of
language by creating user defined data types. programming
represents the needed information in the programData Abstraction also
the details. without presenting
Abstraction refers to the act of representing
without including the background details or essential features
them. explanation between
For example, a class Car
would be made up of an Engine,
Gearbox, Steering objects, and many
Car class, one more
does not need to know how components. build the
To
work internally, but only how to the different
messages to them, receive interface with them,components
i.e., send
the different objects messages from them, and perhaps make
composing
Q.9. What is software
the class interact with
each other.
A reusability. Explain.
definition software reuse is the process of creating
software systems
of
The advantage offrom predefined software
(1) The software reuse: componernts.
(2) The systematic development of reusable
createsystematic
new systems.reuse of these
components components.
as building blockst
two float numbers 19.95, 20 56
Enter
19.95.
onswapping:20.56,
Explain different
Q 19. What do you mean by polymorphism ?
types of polymorphisms ?
Polymorphism:
Folymorphism means "one name, multiple forms". The term
c means the ability to have diferent objects derived from a
nmonclasS respond to the same command differently. An example
of how polymorphism is used every day in the
may point to each student and ask real world, a teacher
the question, "What is your
name"?, and expect adiferent response from each individual. In
other words, the teacher wants to
of students and send the "iterate" across an array (or) list
same message
different objects (the students) and have("What is
your name") to
each student handle the
message differently.
Polymorphism
Parent
Child 1 Child 2
Grand child
mean by inheritance ?
0. 13. What do you
one object car
"Inheritance": Inheritance is the process of
acquire the properties of another.
Parent
Child class
(or) derived features
class. Child's
features
Shape
is a is a
is a
public:
void some_function( )
...
class base2
function properties. abstraction, encex P usefulness
data and of data score over the
concepts
implementing theThesefeatures of a 'class
from data hiding. in obiect oriented programming.
operations
ofstructure ofstructure used for
procedure-oriented
A structure is mostlv object-oriented programming
implement
while aclass is used to changes in onepart of the program donot
Moreever, it ensures that
require changes in other parts of the program.
0.6. WWhat are constructors ? Explain.
Constructors : constructor is a special member function,
A
It is special
whose task is to initialize the objects of its class. constructor is
because its name is thesame as the class namne. The
invoked whenever an object of its associated class is created. It is
of
called constructor because it construct the values of data members
the class. A constructor is declared and defined as follows.
class integer
int m, n;
public :
integer (void);
1/ constructor defined
integer :: integer (void)
m=0;
d n=0;
nd
PROGRAMMING WITHG&Gtt pA0GRAMMING WITH C& C++
115
purpose of
The main to (3) Inheritance: Inheritance is the process of forming a new
the design, programming and mostimportantly
particular data, it is
Soto modify aadditional
easy to debugging prsoimgrplam.fy
identify which,a class from an existing class or base class.
wherefuhcttoioaddn
features it is easy to identify The base class is also known as parent class or super class, the
to use. To add data.
functions and its related new class that is formed is called derived class.
elements of
Following are the basic Object Derived class is also known as a child class or sub class.
Programming (0OPS) :
Inheritance ;(4)
Oriented Inheritance helps in reducing the overallcode size of the program,
(1) Object;(2) Classes; (3) Dynamic which is an important concept in object-oriented programming.
Encapsulation Binding
(5) Polvmorphism ; (6) Message pssing ;(7) of
It is classified into different types, they are - (i) Single level
the basic unit inheritance; (ii) Multi-level inheritance ; (iii) Hybrid inheri
(1) Object: Object is
Objects are identified by its unique name. object-oriented
represents a particular instánce of a class. There can be more An
programming. thanobjoneect
tance ; (iv) Hierarchial inheritance.
(4) Dynamic binding: It contains a concept of Inheritance and
instance of an object can hold its
instance of an object. Each own Polymorphism.
relevant data.
(5) Polymorphism :
An Object is a collection of data members and associated variables of different typesPolymorphism allows routines to use
at different times. An
member functions also known as methods. function can be given different meanings or operator or
For example whenever a class name is created according to the refers to asingle function or functions. Polymorphism
class an object should be created without creating object can't able to different ways. multi-functioning operator performing in
use class. Poly a Greek term ability to take
P lace of Dog defines all possible dogs by listing ne more than one form.
Overloading 1S one type of Polymorphism. It allows an obiect to have
characteristics and behaviors they can have; the object Lassie is one different meanings,
particular dog, with particular .versions of the
charateristics. A depending on its context. When an exiting
operator or function begins to operate on new data
Dog has fur; Lassie has brown-and-white fur. understood to be overloaded. type, or class, it is
objects afe
(2) Class: Classes are data types based on which Data
grouped and its
created. Objects with similar properties and methods areindividual
abstraction
functions into
&
Encapsulation
a single
: The
wrapping up of data
represents a set of unit is called
together toform aClass. Thus a Class
objects. Characteristics of an object are represented in
a class as When
it is only using Data Encapsulation, data is not
Encapsulation.
P'roperties. The actions that can be performed by objects become accessible throughthe functions present accessed directly,
Data
Abstraction inside the class.
functions of the class and is referred to as Methods. increases the power of programming
under Wni language by creating user defined data
For example consider we have a class of cars sTepresents the needed information in thetypes. Data Abstraction also
Objects. In Abi
Santro Xing, Alto and WaganR represents individual year of the details. program without
context each Car Object will have its own, model, presenting
84 PROGRAMMING WITH C& C, PA
For example:
int var = 100; The memory location of var is 100024. Q.2
100024
100024 100
pvar var variabl
The data item is variable. 'var' can be
accessed variabl
the indirety Writ t
write
expression "pvar, where * is a called
unary operator
0tollouws the top-c++
pon apPYoach Bottor. otlow the
o does Alot Botto-up opproa
Kopport ton dons onon
tooePor
Ove Lood?nc
ouerloadnq
c you odt
ant ute in ctt
Btrote
does ot uppo ett
&uppots
ference vaiableeerence vavia ble
epexatoY overlbaorop etatoy overloo/R
&ot poss?ble Pos PoAs? ble n
arns ae
char c; char c;
int x:
jnt X;
ffoat y: float y:
Char
nt
value at a
4 Only once membercan be assigned
4. Althe members can be assigned values at time.
atime. may cause
Value assigned to one membermembers.
5.
5. Value assigned to one member will not the change in value of other initialized
cause the change in other members. Only one union member
can be
6. All members of astructure can be at a time.
Initialized at the same time. union un
struct st
int x;
int x; float y;
float y:
union un x = 12:
when
Struct st s= (10, 12): usage of union is efficient
th The required to be
when all members of it are nottime.
1.\Tmembers
he are actively used in the program.
usage of structure is efficient
accessed at the same
of Structures
2.3.3 Array
A
Q30. Explain thè concept of Array of structures.
Answer :
Model Papar-\, Q8a
Declaration
Arayof
is aArray of Structures
collection of similar data types. Grouping structure variables into one array is referredIto as an array of structures
int marks1:
int marks2;
int marks3;
}st[3):
.
Where st[3]is an array of 3 elements containing three objects of student structure 1.e., each element st[3] has struchra
student with three members that are marksl, marks2 and marks3.
Usage of Array of Structures
Structure is a collection of dissimilar data types, for example in a structure for representing student information, the ideil
memhers include studentnamne, rollno, marks and it can be programmatically repre sented as shown below.
struct student
char studentname[30];
int rollno;
int marks;
char sname[20];
int rolino;
fioat average,
main( )
int i;
struct student st[3] ={{"XXX", 0501,
79.6}, {"YYY, 0502, 80.3},
for(i =0;i<<2; it+) {"ZZZ", 0S03. 96.25}:
char a = 100;
char b=28;
int e;
char d;
C=a+b:
d=a+b;
printf( "c=%d, d = %dn", c, d);
The output is:
C=128, d=-128
The addition operator
this environment a char is a performs implicit integer
signed 8 bit value, which promotion. On
-128 to 127. On line 10, though both a and b are has a range of
chars
converted into int before the addition is done, then the they are
result is
assigned to c, On line 11 the same thing happens, but the int result of
128 is converted báck into a char, which causes the value to
"wrap
around" to -128 since a positive 128 is not within the range of a char.