Oop Notes
Oop Notes
1 INTRODUCTION TO C++
NAMESPACE SYNTAX
NAMESPACE IDENTIFIER
{
ENTITIES DECLARATION
}
EXAMPLE
Namespace mynamespace
{
int a,b,c;
}
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
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-
C++ VARIABLES
it is a memory location that holds data that can be changed during
execution of the program.
instance variable
-variable declared in a class but outside of constructors methods.
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.)
2 DATA FILE
-have .dat extension.
-is computer readable.
-printable on screen.
-stores data in binary format.
-opened using ios::binary statement.
DOCUMENTATION IN C++
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.
syntax
data_type*ptr=new datatype(value);
example
int*ptr=new int(100);//4 bytes
PRIVATE
-allow access within the same clas
-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.
-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.
-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.
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;
-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.