UNIT - 2UNIT - 2
Mrs. Pranali P. Chaudhari
Contents
Namespace
Data Types
Reference Variables
this pointerthis pointer
Classes and Objects
Access Specifiers
Defining data members and member functions
Array of Objects
Namespace
Namespace defines a scope for the identifiers that are
used in the program.
Std is a namespace where ANSI C++ standard class
libraries are defined.
We can define our own namespace in our program.We can define our own namespace in our program.
Syntax:
namespace namespace_name
{
// declaration of variables, functions, classes etc.
}
Namespace
Example:
namespace Testspace
{
int m;
void display (int n)void display (int n)
{
cout<< n;
}
}
To assign value to m we use scope resolution operator as,
Testspace :: m = 100 ;
Tokens
Tokens: the smallest individual units in a program are
known as tokens.
Keywords ( int, char, new, delete, etc…..)Keywords ( int, char, new, delete, etc…..)
Identifiers ( a, display, etc…..)
Constants
Strings
Operators
Data Types in C++
C++ Data Types
User Defined types
Structure
Build – in types Derived Types
ArrayStructure
Union
Class
Enumeration
Array
Function
Pointer
Reference
Integral Type Void Floating Type
int Char float double
Enumerated Data Type
It is a user defined data type which provides a way for
attaching names to numbers.
For eg: enum shape {circle, square, triangle}For eg: enum shape {circle, square, triangle}
enum color {red, blue, green}
In C++ the tag name becomes new data type.
For eg: shape ellipse;
Quiz 1
What is the difference between a variable and
constant?
The difference between variables and constants is that
variables can change their value at any time but
constants can never change their value.
Variables
C++ allows declaration of variables anywhere in the
scope.
For eg: float average;For eg: float average;
average = sum/i ;
C++ permits initialization of the variables at run time.
// Dynamic initialization
For eg: float average =sum/i ;
Reference Variables
C++ defines a reference variable that provide an alias
for a previously defined variable.
Syntax:Syntax:
data- type &reference-name = variable-name
It is used to passing arguments to functions
For eg: float total = 100;
float & sum = total;
Reference Variable
Major application of reference variables is in passing
arguments to functions.
void f (int & x) // uses reference
{{
x = x + 10; // x is incremented so also m
}
int main()
{
int m = 10;
f (m); // function call
}
Operators in C++
C++ has a rich set of operator:
Insertion Operator <<
Extraction Operator >>
Scope Resolution Operator ::
Pointer-to-member declarator ::*Pointer-to-member declarator ::*
Pointer-to-member operator ->*
Pointer-to-member operator .*
Memory release operator delete
Memory allocation operator new
Line feed operator endlendl
Field width operator setwsetw
Scope Resolution Operator
Scope resolution operator allows access to global version of
a variable.
For eg:
::m will always refer to the global m.::m will always refer to the global m.
Major application of the scope resolution operator is in
classes to identify the class to which a member function
belongs.
For eg:
void employee :: getdata(void)
int item :: count
Memory Management Operator
C++ defines new and delete operators for allocating and freeing the
memory .
An object is created by using new, and destroyed by using delete as
and when required.
Syntax:
pointer-variable = new data-type;
int *p = new int; *p = 25;
pointer-variable = new data-type(value);
int *p = new int(25);
pointer-variable = new data-type[size]
int *p = new int [10];
delete pointer-variable;
delete p;
Manipulators
Manipulators are the operators that are used to format
the data display.
Commonly used manipulators are endl and setw.Commonly used manipulators are endl and setw.
endl manipulator causes a line feed to be inserted.
setw specifies a field width for printing the value of the
variable.
Type Cast Operator
C++ permits explicit type conversion of variables using
type cast operator.
average = sum / (float) i ; // C notationaverage = sum / (float) i ; // C notation
average = sum / float (i) ; // C ++ notation
A type name behaves as if it is a function for
converting values to a designated type.
this pointer
C++ uses a unique keyword this to represent an object
that invoke a member function.
this is a pointer that points to the object for which thisthis is a pointer that points to the object for which this
function was called.
For example:
A call to a function A.max() will set the pointer this to
the address of the object A.
Quiz 2
Define Class.
A class is a collection of objects of similar type.
What are the contents of class?
The class consists of data members and member
functions. Both of which characterise the object
for which the class is defined.
Classes and Objects
A class is a way to bind the data and its associated functions
together.
It allows the data to be hidden, if necessary, from external
use.use.
When defining a class a new ADT is created that can be used
like any other built-in type.
A class have two parts:
Class declaration
Class function definitions
Class Declaration
General form of class declaration:
class class_name
{
private :private :
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
Class Example
class item
{
int number;
float cost;float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
Quiz 3
What are Access Specifiers?
Access modifiers (or access specifiers)
are keywords in object-oriented languages that set
the accessibility of classes, methods, and other
members.
Accessing Class Members
We have three basic access types:
private
public
protected
Private members can be accessed within the class itself.
Public members can be accessed from outside the class.
A protected access specifier is a stage between private
and public access. If member functions defined in a class
are protected, they cannot be accessed from outside the
class but can be accessed from the derived class.
Accessing Class Members
The main() cannot contain the statements that access
private data members directly.
They use the member function to use them.
For eg:
x.getdata (100, 75.5)x.getdata (100, 75.5)
x.number = 100; // illegal
Objects communicate by sending and receiving
messages.
For eg:
x.putdata();
Defining Member Functions
Member functions can be defined in two phases:
Outside the class definition
Inside the class definition
A member function can also be private
A private member function can only be called by
another function that is a member of its class.
Static Data Members
A data member of a class can be static.
A static member variable has the following characteristics:
It is initialized to zero when the first object of its class isIt is initialized to zero when the first object of its class is
created. No other initialization is permitted.
Only one copy of that member is created for the entire class.
It is visible only within the class
Example
Static Members Functions
A static member function has following characteristics:
A static function can have access to only static members
(functions or variables) declared in the same class.(functions or variables) declared in the same class.
A static member function can be called using the class
name as:
class-name :: function-name;
Eg: item :: showcount();
Example
Quiz 4
Define Array.
Array is a data structure used to store the elementsArray is a data structure used to store the elements
of same type.
Array of Objects
Similar to structure we can create a array of objects for a
class.
For eg:
employee manager[5];
employee worker[10];
Example
Summary
________ defines a scope for the identifiers that are used
in the program.
Main purpose of defining reference variable is ______.
C++ defines ______ and _____ operators for allocating
and freeing the memory .and freeing the memory .
The main() cannot contain the statements that access
private data members directly. (True/False)
A private member function can only be called by
another function that is a member of its class.
(True/False)
A static member function can be called using ______ .
References
Object Oriented Programming with C++ by E.
Balagurusamy.Balagurusamy.
Introduction to C++