3. Structure of a Program
#include
<iostream>
- Lines beginning with a hash sign (#) are directives
for the preprocessor.
- In this case the directive #include<iostream> tells
the preprocessor to include the iostream standard
file.
- This specific file (iostream) includes the declarations
of the basic standard input-output library in C++.
3 16 August 2012
4. Structure of a Program
using namespace
std;
- All the elements of the standard C++ library are declared
within what is called a namespace, the namespace with
the name std.
int main()
- This line corresponds to the beginning of the definition of the
main function.
- The main function is the point by where all C++ programs start
their execution, independently of its location within the source code.
4 16 August 2012
5. Structure of a Program
cout<<“Hello World”;
cout is the name of the standard output stream in C++, and the
meaning of the entire statement is to insert a sequence of
characters into the standard output stream (cout, which usually
corresponds to the screen).
cout is declared in the iostream standard file within
return 0;
the std namespace
The return statement causes the main function to finish.
5 16 August 2012
7. Basic input/output in C++
Standard Output
By default, the standard output of a program is the screen, and the
C++ stream object defined to access it is cout.
cout is used in conjunction with the insertion operator, which is
written as << (two "less than" signs).
7 16 August 2012
8. Basic input/output in C++
Standard Input
The standard input device is usually the keyboard. Handling the
standard input in C++ is done by applying the overloaded operator
of extraction (>>) on the cin stream.
The operator must be followed by the variable that will store the
data that is going to be extracted from the stream. For example:
8 16 August 2012
9. Introduction
A class is an extension of the idea of structure used in ‘C’.
It is a new way of creating and implementing user defined data
types.
Structure : Member are public(By Default)
Class: Members are private (By Default)
9 16 August 2012
10. Class specification
When we are creating a class, we are creating a new abstract data
types that can be treated like any other built in data types.
Class declaration describes the type and scope of its member.
Class function definitions describe how the class functions are
implemented?
Class Specification=Class declaration + Class
function definition
10 16 August 2012
12. Private and public
The data is hidden so it will be safe from accidental manipulation,
while the functions that operate on the data are public so they can
be accessed from outside the class.
12 16 August 2012
13. A Simple Class
Data members are usually declared as private and member
functions as public
13 16 August 2012
14. Object Creation
Creates a variable s1 of type smallobj.
smallobj s1 ; // Memory for x is created.
Note-: In C++, the class variables are known as objects . Therefore
x is called an object of type smallobj.
We may also declare more than one objects in a single
statements.
Smallobj s1, s2, s3;
14 16 August 2012
16. Accessing Class members
A variable declared as public can be accessed by the objects
directly.
16 16 August 2012
17. Accessing Class members
A variable declared as public can be accessed by the objects
directly.
17 16 August 2012
18. Defining Member function
Defining
Member
Function
Outside the Inside the
class class
definition definition
18 16 August 2012
19. Member Functions Within Class Definition
We can define the member function with the class to replace the
function declaration by the actual function definition inside the class.
Inline
function
19 16 August 2012
20. Member Functions outside Class Definition
Function that are declared inside a class have to be defined
separately outside the class.
20 16 August 2012
22. Member Functions :Some Special characteristics
Several different classes can use the same function name.
Membership level will resolve the issue.
Membership function can access the private data of the class. A
non member function can not do so.
- Friend function is the exception of this
A member function can call another member function directly
without using dot operator.
22 16 August 2012
26. Nested Function
A member function can be called by using its
name inside another function declaration
26 16 August 2012
27. Private member function
Normal practices:
To place all the data items in a private section
and all the functions in public section
But some situations may require certain functions to
be hidden
27 16 August 2012
28. Private member function(Cont…)
A private member function can only be called by
another function that is a member of its class.
Note: Even an object cannot invoke a private
member function using dot operator
28 16 August 2012
29. Private member function(Cont…)
If S1 is an object of class sample, then
S1.read(); //illegal , object can not access the private
member
29 16 August 2012
30. Private member function(Cont…)
te
Function read() can be called by the function update()
to update the value of m.
30 16 August 2012
31. Memory allocation for objects
Memory space for object is allocated when they are declared and
not when the class is specified.
Member functions are created and placed in the memory space
only once when they are defined as apart of class specification.
Since all the objects belonging to that class use the same member
functions, no separate space is allocated for member functions
when the objects are created.
Only space for the member variable is allocated separately for each
object
31 16 August 2012
32. Memory allocation for objects
Member Function1 Memory
Created
when function
Member Function2 is defined
Object 1 Object 2
Var1
Var1
Var2 Var2
32 16 August 2012
33. Static Data members
In general, each object contains its own separate data.
If a data item in a class is declared as static , only one such item is
created for the entire class, no matter how many objects there are.
A static data item is useful when all objects of the same class must
share a common item of information
Static member variable is visible only within the class, but its
lifetime is the entire program.
33 16 August 2012
35. Static Data members
Note:
The Type and scope of each static member variable must be
defined outside the class definition.
Because the static data members are stored separately rather than
as a part of an object.
Static variables are normally used to maintain values common to
the entire class
35 16 August 2012
36. Static data member
Object 1 Object 2
Var1 Var1
Var2 Var2
Static data Common to all
variable the objects
36 16 August 2012
37. Static function
A static function can have access to only other static members (
functions or variables) declared in the same class .
A static member function can be called using the class name (
instead of its objects)
Class-name :: function-name;
However it can be called by objects of the class also.
Static member can also be defined in the private region of a class
also.
37 16 August 2012
39. Objects as function arguments
It is possible to have functions which accepts objects of a class
as arguments, just as there are functions which accept other
variables as arguments.
Pass-by-value Pass-by-reference
A copy of entire object is Only the address of the
passed to the function objects is passed to the
Any modification made to function.
the object inside the Any changes made to the
function is not reflected in object inside the function is
the object used to call a reflected in the actual
function object.
39 16 August 2012