OBJECT ORIENTED
PROGRAMING USING C++
NOTES..
5
Programing and Programing languge,
• programing is to give the machine a list of steps to perform particular task-
• A digital system like computer understands only binary languge (which contains o's and i’s),also called a machine
languge.
• but programing in machine Lanquage is almost imposible for human being, hence manufuctures of the procedure
develop assembly languge, it is simple than machine languge but makes huge use of Software.
• To remove Complexity of programing high level languge (HLL) were [Link] lanquges are COBOL, C, C++ JAVA
etc.
BINARY NUMBER SYSTEM:
• Binary number system can have only two representation namely ‘0’ and ‘1’ for all number and character.
• DECIMAL NUMBER: 5, 1,7,9.
• BINARY NUMBER: 1011,1110,0011.
5
DECIMAL TO BINARY conversions:
• steps:
• i)Divide the decimal number by 2.
• ii)Note down the quotient and reminder.
• Iii)Repeat the above procedure till the quotient is zero.
• iv) the last reminder is the MSB( most significants ie bit with highest weight) and the first reminder the LSB( least
Sgnificant bit)
• Example
(5)10 = (?)2
BINARY TO DECIMAL:
• Multiply the binary digits with power of two according to their positional weight.
• The position of the first bit is 0 and it keep on increasing.
• Example
(11100)2 = (?)10
=1x24 +1x23 +1x22 +0x21 +0x20
=16+8+4+0+0
=[28]
There are various oprations that are performed on binary data like AND,OR,EXOR,NOT etc.
5
1)AND:
The output is 1, if and only if A AND B (A&B being the inputs) are 1.
A (input) B(input) Y(output)
0 0 0
0 1 0
1 0 0
1 1 1
2) OR:
The output is 1, if and only if A OR B (A&B being the inputs) are 1.
A (input) B(input) Y(output)
0 0 0
0 1 1
1 0 1
1 1 1
3) EXOR:
The output is 1, if and only if Exclusively A OR B (A&B being the inputs) are 1.
A (input) B(input) Y(output)
0 0 0
0 1 1
1 0 1
1 1 0
5
4) NOT:
The output is not A, (A being the input) are i.e complement of A
A (input) Y(output)
0 1
1 0
Procedure oriented programming:
Procedure oriented programming mainly focus on method or procedure
• In these programming large programs divded into smaller part using function
• Data can be accessed by any function, and hence there is scope of unexpected change of data, global data is allowed hence
data is not hidden.
• If changes are done in some data the corresponding changes are to be implemented in all the functions using the data
• It follow top-down approach in program design
• It does not used to solve real life problems
• C programming language is an example of procedure oriented programming.
5
object oriented programming
• programs a have classes and objects, only the functions t can access the data in a given obiect
• Data Can be accessed only by functions of that class and hence it cannot be changed unexpectly
• in Oops No global data, data is encapsulated
• Adding of new dato or changes in the data requires to change only the functions of that class.
• it follows bottom-up approch in program design - it is used to model the real world problems .
• C++, Java, Authon, visuel, .net are example of Object Oriented programing .
Application of object onented proaming
• JReal-time System.
• Simulahon and modeling
• Software bevelopment
• Game Development.
• operating Systems and divers
• GUI Applicakons
• Enterprise Applications
• Database management destem.
5
Data types:
• The data type decides the type of data and memory location
This Photo by Unknown Author is licensed under CC BY-SA-NC
5
Identifiers :
• identifiers are names given to different user defined things like variables, constants, functions, classes, objects, structures, unions,
etc.
While making these identifiers we need to follow some rules.
These rules are stated below:
• 1. The identifier can consist of alphabets, digits and a special symbol i.e. '_' (underscore).
• 2. An identifier cannot start with a digit. It can start either with an alphabet or underscore.
• 3. It cannot contain any special symbol except underscore. Blank spaces are also not allowed.
• 4. It cannot be a keyword.
• 5. It is case sensitive i.e. an alphabet capital in one identifier with same name in another identifier with that alphabet small case will
be considered different .
• 6. an identifier can be as long as required and minimum of one character.
For examples:
1. simple_interest: Valid
2. char: Invalid, because it is a keyword.
3. 3friends: Invalid, because starts with a digit.
1. _3friends: Valid
2. Simple interest: Invalid, because blank spaces are not allowed.
3. #3friends: Invalid, because no special symbol except underscore is allowed.
4. void: Invalid, because keyword not allowed. 7.
5. Void: Valid, case sensitive.
5
Declaration of Variables and constants
• Constants are used to declare the values that remain constant, for e.g. value of pi.
• Constants can be defined either by writing the keyword const before the data type or by using #define.
• variables are values given to identifiers that can change their values during the execution of the program
• A variable can be defined with the data type.
• For example
• For declaring a integer data type the syntax is: int x;
• . For declaring rate of interest as float data type the syntax is: float rate;
• For declaring a character data type element the syntax is: char x;
Escape Sequences:
• Escape sequence is a character followed by a backslash
• .1. \n Newline
• 2. \t Horizontal Tab
• 3. \v Vertical Tab
• 4. b Backspace
• 5. \r Carriage Return6. \f Form feed
• 7. \a Audible Alert (bell)
• 8. \\ Backslash
• 9. \? Question mark
• 10. ' Single quote
• 11. " Double quote
structure of a C++ program
C++ Header
Class definition
Member functions definition
Main function
Q) Write is a simple program to display a statement "Hello Friend".
#include<iostream>
Using namespace std;
int main(){
cout<<"Hello Friend";
return 0
}
Output :Hello Friend
5
Oprators
.1 Unary Operators: these oprator requires one operand;
1. Unary minus (-):
The symbol shown in the bracket is used as unary minus operator.
It returns the negative value of the variable to which it is preceded.
For e.g. if x=3 and y=6 theny = -x;
will make the value of y as - 3
2. Casting Operator .
The casting operator is used for type conversios, It can be used to convert a data of one type another data type.
For e.g. if we have int x = 3 float y = 5.6
then the statement, x=(int) y;
will result in the value of x as 3.
3. Logical Not operator (!)
the exclamation mark is used as a logical not operator.
It is used to check certain conditions in a condition statement.
It performs the logical nigation.
.For e.g.x = 1 then y =!x;
will result with y having the value 0. 5
4)Address of operator (&):
This operator returns the address of the variable associated with it.
[Link] e.g. if we write y = &x;
then the memory address allocated to the variable x will be copied into the variable y. For this the variable y must be a
pointer variable.
5)Scope Resolution operator (::)
(It resolves the scope of a variable to be inside the function or outside the function. It is denoted by duble colon
.For eg
if an external variable x is to be accessed then the same can be accessed using the scope resolution operator by the statement,
Example:
#include <iostream>
char a = 5;
'int b = 50
int main(){
char a=67;
'cout<< "The value global variable b is: "<<::b;
cout<<"\n The value of local variable a is: " << a;
cout<<"\n The value of global variable a is: " <<:: a;
return 0;
} 5
6. Size of operator:
• These operator is used to know the size of a variable as required to store its value in memory.
• It can also be used to find the size of a data type
For e.g, the statement size of (int);
will return the value 2, as int requires 2 bytes.
Another e.g.: int x, y;
• Char a;
• X=sizeof(a);
• Y=sizeof(float),then x will be 1 and y have 4.
7)Indirection operator or value of operator (*)
• This operator returns the value of the data at the given address
• .For e.g.z =^ * y;will give the value of the data stored at the address given hy y
8)Increment Operator (++)
• It returns the value of the variable added with one and stores the result in the variable itself
• .For e.g.x = 5 then x ++will make the value of x equal to 6.
• This "x++;" (also called as post increment operator) can also be written as "++x;"
• (also called as pre increment operator).
5
9)Decrement Operator(--):
It returns the value of the variable subtracted with one and stores the result in the variable itself
.For e.g. if x = 5, then x--;will make the value of x equal to 4.
This "x--;" (also called as post decrement operator) can also be written as "--x;" (also called as pre decrement operator.