You are on page 1of 8

oop notes

1 INTRODUCTION TO C++

STRUCTURE OF A C++ PROGRAM

#include<iostream.h> -PRE-PROCESSOR DIRECTIVE that causes preprocessor to


add contents of the i/o stream file to the program.

using namespace std; -it allow user to group entities such as


classes,objects and functions under one name.
int main() -it tells the compiler where to start the execution from.
{
cout<<""<<endl;
return 0;
}

REASONS FOR USING NAMESPACE IN C++PROGRAM


it reduces the name collision by organizing codes into logical groups
especially when your base code includes multiple libraries.
it provides space where we can define or declare identifiers such as
variables,functions and classes.
it allows grouping of entities such as classes,objects and functions under
one name,otherwise the local scope can be divided into sub-scope each one
with its own name.

NAMESPACE SYNTAX

NAMESPACE IDENTIFIER
{
ENTITIES DECLARATION
}

EXAMPLE
Namespace mynamespace
{
int a,b,c;
}

DEFINE DECLARATION- it is a language construction specifying identifier


properties,TOKENS-name given to various functions in c++ program,

C++ TOKENS

c++ keywords
(reserved words that has predefined meaning)they include
protected,private,public,throw,try,catch,new,delete,virtual,inline,friend.

identifiers
-name given to entities such as functions,variables,classes and
structure.ie int money
int-variable type
money-identifier denoting variable type integer

RULES FOR NAMING IDENTIFIERS


1 It should start with a letter or underscore only.
2 can be composed of alphanumeric characters and underscore .
3 should not contain blank or white space .
4 there is no rule for the length of an identifier.
5 reserved words should not be used as identifiers.

constants/literals-

RULES OF CONSTANTS/LITERALS
1 Can be preceded by minus sign if required
2 commas and blank spaces are not permitted
3 its value must be within its minimum bounds of specified data type

comments-

circumstances of using comments in a c++ program code

.when one need to explain a section of code that may be difficult to


understand.
.used when there is need to enhance program readability.
.when you want to prevent execution when testing alternative code.

C++ VARIABLES
it is a memory location that holds data that can be changed during
execution of the program.

RULES WHEN NAMING VARIABLES


1 should start with a letter or underscore only.
2 must not contain white or blank spaces.
3 reserved words should not be used as variable name.
4 must be unique in a given scope.

instance variable
-variable declared in a class but outside of constructors methods.

syntax of instance variable

Data_type VariableName
EXAMPLE
Class Request
{
int age;
};
VARIABLE SCOPE

LOCAL VARIABLE
variable which exist only between curly braces in which it is declared.
outside that they are unavailable and leads to compile time error.

EXAMPLE
#include<iostream.h>
using namespace std;
int main()
{
int i=18;-initialization and declaration
if(i<36)-if condition starts
{
int n=50;
}-if condition ends
cout<<n;-compile time error

GLOBAL VARIABLE
variable declared outside int main()function.
are declared once and used through out the program lifetime by any class or
function.
if declared and initialized it can be assigned any value at any given point
of the program otherwise if declared only they can be assined different
values at different time in the program lifetime.

EXAMPLE
#include<iostream.h>
using namespace std;
int x-declaration
int main()
{
x=10;-initialization
cout<<""<<endl
x=30;-init
cout<<""<<endl
};

C++ TEMPLATES
class template/template class-is a blueprint of creating generic
class/function.it allows a program to operate with generic data type.are
well suited for developing containers(objects designed for encapsulating
other objects of any type.)

stream class/class stream-is the stream of characters that are transferred


between program thread and i/o.used to input operations on the files and
i/o devices.are appropriate when defining an interface that works on
multiple types of unrelated objects.

EXCEPTION HANDLING IN C++


ERROR HANDLING TECHNIQUES IN C++
TRY-allow you to define a block of code to be tested for errors while it is
being executed.

THROW-throws an exception when aproblem is detected which lets us create


custom error.

CATCH-allow us to define a block of code to be executed if an error occurs


in the try block.

TYPES OF EXCEPTION IN C++


SYNCHRONOUS EXCEPTION-errors such as "out-of-range index & over flows".
ASYNCHRONOUS EXCEPTION-errors generated by events beyond the control of the
program.
FILE ORGANIZATION IN C++
1 TEXT FILE
-have .txt extension
-can be read by humans
-interprates characters
-stores data in form of ASCII characters
-opened in i/o mode with the use of ofstream

2 DATA FILE
-have .dat extension.
-is computer readable.
-printable on screen.
-stores data in binary format.
-opened using ios::binary statement.

REASONS FOR FILE OPERATION FAILURE DURING PROGRAM EXECUTION


.specifying wrong file name.
.using file that is not opened.
.using file that is not in appropriate mode.
.file restriction properties such as read only files cannot be modified.

IMPORTANCE OF FILES IN C++/REASONS FOR USING I/O FILES IN C++


-facilitates movement of data from one computer to another without changes.
-it facilitates easy access of data by use of c++ commands.
-used for storing data in storage devices permanently to avoid loss.
-Handles data that has several operations to be performed,hence saves time
when entering large amount of data.

FILE STREAMS IN C++


Ifsteam-used for reading information from the file.
ofstream-used for creating a file and writing information to the file.
fsteam-used for creating files,writing information to the filess and
reading information from the file.

PROCESS THAT CAN BE CARRIED OUT IN AN EXISTING FILE


openning a file
reading a file
writing a file
closing a file
appending a file

MODES FOR ACCESSING A FILE IN C++


open mode
read mode
write mode
close mode

FILE MODES PARAMETERS


Ios::beg-seek file from the beginning.
Ios::cur-seek file from the current location.
Ios::end-seek file from the end.
VALUES OF TYPE OPEN MODE IN C++/VALUES ASSOCIATED WITH OPEN MODE IN C++
Ios::ate-opens afile for output and moves R/W control to the end of the
file.
Ios::in-opens a file for reading.
Ios::out-opens a file for writing.
Ios::trunc-truncates the contents of an existing file before openning it.
Ios::app-
Ios::binary-

OPENING A FILE IN C++


once you have created a stream in c++,one way to associate it with a file
is by using the function open().this is a member function of each of the
stream class;ifstream,ofstream,fstream.

DOCUMENTATION IN C++

USES OF PROGRAM DOCUMENTATION


>used to aid in simplifying the product and cut support cost.
>used to make information easily accessible.
>help new users learn quickly.
>used for providing limited number of users entry point.

TYPES OF DOCUMENTATION
>internal documentation-is a brief user manual that includes comment
section of each function.
>external documentation-is written for software users to understand tools
used by programmers when developing the software.

DYNAMIC MEMORY ALLOCATION IN C++

NEW OPERATOR-used to allocate the required number of bytes of memory at


runtime.

syntax
data_type*ptr=new datatype(value);
example
int*ptr=new int(100);//4 bytes

DELETE OPERATOR-used to release memory allocated using new at any time in


between the program.
syntax
delete ptr;
example
delete p;

MEMORY LEAK-situation where memory blocks gets wasted due to programmer


allocating a certain memory variable then forgetting to deallocate it
properly.

ACCESS MODIFIERS OR SPECIFIERS IN C++


PUBLIC
-allow access within the same class.
-allow access within derived class.
-allows access outside the class.

PROTECTED-allow access within the same class.


-allow access within derived class as protected members.

PRIVATE
-allow access within the same clas

C++ DATA TYPES

1 DERIVED DATA TYPES


-ARRAY DATA TYPE-a set of elements of the same data type that are
reffered by the same name.

-POINTER DATA TYPE -is a variable data type that can store memory address
of another variable.
uses of this pointer
-can be used to declare indexers
-can be used to refer current class instance variable.
-used to pass current object as a parameter to another method.

-REFERENCE DATA TYPE-it provides new name to the already existing


variable.it is dereferenced implicitly and does not need a dereferencing
operator to retrieve the value referenced.

ways of using reference data type


1independent reference can be created.
2can be returned by a function.
3can be passed to a function.

2 USER DEFINED DATA TYPES


-STRUCTURE-is a data type that allows the storage of data in contiguous
areas of memory,of associated data items.
-data type used when you need to store similar or different data types
under a single valuue.
-used when you intend to modify your data after creation.
-used when you need to store elements of similar or different data type
under one data type.
*can instantiate an object without using a keyword.
*structure instance is called structure variable.
*is a value type data type.values are allocated on stack.
*can not have null values.

justifying the existance of structure data type in c++


.are useful when working with different data types.
.it implements public access modifiers by default.

-CLASS-data type that contains both data members and member functions.
-used when you need to manipulate data into one.
-used when there is need to determine the forms of objects.
*can instantiate an object using keyword.
*class instance are called objects.
*is areference type data type.values are allocated on heap.
*can have null values.

justifying excistance of class data type in c++


.it implements oop which is not achieveable in structures.
.it add private access by default which help in data encapsulation.

-NESTED CLASS DATA TYPE-is a data type declared inside another enclosing
class.it is a member of class and it follows the same access rights that
are followed by different members of the class.it has ability to access all
data members of the main class.it is used for grouping classes together to
make the code readable.it is also used for restricting special access from
enclosing ĉlass member towards inner class.

-ENUMERATED-data type consisting of named values like elements that


represents integral constant.
-used when you expect a variable to select one value from the possible set
of values.
-used when you need to focus more on values rather than worrying about how
to store them.(it increases abstraction)
-used when you want to determine and group integral constant.

syntax
enum enumeration_name
{
value1,
value2,
};
example
enum colours
{
red;
blue;
green;
};

-TYPEDEF-data type that allow us to have more meaningful names for existing
data type.it makes it easier to make changes to underlying data type that
you use.

syntax
typedef type new name;

i.e typedef int feet;

3 PRIMARY/SIMPLE/FUNDAMENTAL DATA TYPE


-INT
is an integral whole number without decimal point.
it is used for counting.
-CHAR
stores single charecter within a quote.
can be assigned to variable of any numeric data type.i.e short or long
data type.
escape sequence can be used to write character literals.

-FLOAT
used for measuring quantities.
has decimal point.
is a single precision data type.
has 32 bit floating points according to IEEE.
has precision of 6 decimal places.

-DOUBLE
stores floating point numbers.
it takes 8 bytrs for storage.
is double precision data type.
has 64 bit floating points according to IEEE.
has precision of 15 decimal places.

-BOOLEAN
only true or false values are allowed for boolean literals.
only assigned to variables declared as boolean.

-VOID
used for specifying the retuurn type of a function, when the function is
not returning any value.
can be assigned a pointer value of any basic data type.
only empty parameters list is used for void.

ABSTRACT DATA TYPE


a class for objects whose behaviour is defined by set of values and a set
of operations.

-PROPERTIES OF ABSTRACT DATA TYPE


.exports a set of operation called interface.
.are created by user in libraries.
.axioms and precondition defines the application domain of the type.
.it exports a type.

You might also like