chap 1 oopnotes
chap 1 oopnotes
3. No data hiding
Data Data
Communication
Functions Functions
Object C
Functions
Data
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data
structure.
5. Data is hidden and can’t be accessed by external functions.
Oop offers several benefits to both the program designer and the user. Object-
oriented contributes to the solution of many problems associated with the
development and quality of software products.
The principal advantages are :
1. Through inheritance we can eliminate redundant code and extend the use
of existing classes.
2. We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the code
from scratch. This leads to saving of development time and higher
productivity.
3. This principle of data hiding helps the programmer to build secure
programs that can’t be invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out
any interference.
5. It is easy to partition the work in a project based on objects.
6. Object-oriented systems can be easily upgraded from small to large
systems.
7. Message passing techniques for communication between objects
makes the interface description with external systems much simpler.
8. Software complexity can be easily managed.
APPLICATION OF OOP:
The most popular application of oops up to now, has been in the area of user
interface design such as windows. There are hundreds of windowing systems
developed using oop techniques.
Application of oop includes.
1. Objects
2. Classes
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
OBJECTS
2. They may represent a person, a place, a bank account, a table of data or any
item that the program must handle.
3. The fundamental idea behind object oriented approach is to combine both
data and function into a single unit and these units are called objects.
4. The term objects means a combination of data and program that represent
some real word entity.
5. For example: consider an example named Amit; Amit is 25 years old and
his salary is 2500. The Amit may be represented in a computer program as
an object. The data part of the object would be (name: Amit, age: 25, salary:
2500)
CLASS:
A group of objects that share common properties for data part and
some program part are collectively called as class.
DATA T otal
Name
Date-of-birth
Marks Av erage
FUNCTIONS
Total
Average isplay
Display
DATA ABSTRACTION :
DATA ENCAPSALATION :
The wrapping up of data and function into a single unit (called class)
is known as encapsulation. The data is not accessible to the outside world
and only those functions which are wrapped in the class can access it. These
functions provide the interface between the objects data and the program.
INHERITENCE :
POLYMORPHISIM:
2. The behaviour depends upon the type of data used in the operation.
3. A language feature that allows a function or operator to be given more
than one definition. The types of the arguments with which the function
or operator is called determines which definition will be used.
4. Overloading may be operator overloading or function overloading.
5. Function overloading means a function with same name and different
parameters.
DYNAMIC BINDING :
Object Information
Message
C ++ Data Types
float double
int char
Data types in C++ can be classified under various categories.
usigned 1 0 to 265
value.
2) To indicate an empty argument list to a function.
Example:
void function(void);
Variables:
Defination - A named memory location is called variable.
OR
It is an identifier used to store the value of particular data type in the
memory.
Since variable name is identifier we use following rules which are same as of
identifier
Identifiers may have any length but only first 31 characters are
significant.
Variable declaration: The declaration of variable gives the name for memory
location and its size and specifies the range of value that can be stored in that
location.
Syntax: Data type variable name;
Ex: int a;
REFERENCE VARIABLES:
Synatx:
Datatype & reference –name=variable name;
Example:
float total=1500;
float &sum=total;
Here sum is the alternative name for variables total, both the variables refer to
the same data object in the memory .
A reference variable must be initialized at the time of declaration .
Note that C++ assigns additional meaning to the symbol & here & is not an
address operator
.The notation float & means reference to float.
Example:
int n[10];
int &x=n[10];
char &a=’\n’;
type casting :- refers notes
OPERATORS AND EXPRESSIONS
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
6. Conditional operator
8. unary operator
9. Special operators
OPERATORS IN C++ :
C++ has a rich set of operators. All C operators are valid in C++
also. In addition. C++ introduces some new operators.
output:
Local a=10 Global a=5
Manipulators:
Manipulators are the operators used to format the data that is to be
displayed on screen.
The most commonly used manipulators are endl and setw
1.endl:-it is used in output statement and inserts a line feed. It is similar to new
line character (“\n”) ex:
………………..
cout<<”a=2”<<endl; cout<”name=sunil”<<endl;
……………….
C++ defines two unary operators new and delete that perform the task of
allocating and freeing the memory in a better and easier way.
1. New :- The new operator can be used to create objects of any type.
Example:
delete p;
For example:-refer lecture notes
Basics of C++ :-
C ++ is an object oriented programming language, C ++ was developed by
Jarney Stroustrup at AT & T Bell lab,
USA in early eighties. C ++ was developed from c and simula 67 language.
C ++ was early called ‘C with classes’.
C++ Comments:
// this is an example of
// c++ program
// thank you
The c comment symbols /* ….*/ are still valid and more suitable for multi
line comments.
The statement cout <<”Hello, world” displayed the string with in quotes on
the screen.
The identifier cout can be used to display individual characters, strings and
even numbers.
It is a predefined object that corresponds to the standard output stream.
Stream just refers to a flow of data and the standard Output stream normally
flows to the screen display. The cout object, whose properties are defined in
<iostream.h >represents that stream.
The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
Ex:- cout<<”\n Welcome to CPP”;
Input Operator:
Return Statement:
Insertion (<<) and Extraction (>>) operators: the operators are use
with output and input objects ex:
cout<<”Enter n”;
cin>>n;
Cascading Of I/O Operator:
If single statement contains more than onr I/O opertaors then it is called
Cascading Of I/O Operator.
cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average
=”<<average<<”\n”;
cin>>number1>>number2;
Structure Of A Program :
Probably the best way to start learning a programming language is by
writing a program. Therefore, here is our first program:
// my first program in C++
#includ <iostream.h>
void main ()
{
cout <<
"Hello
World!";
}
Output:-Hello World!
# include<iostream.h>
class person
{
char name[30]; int age;
public:
void getdata(void); void display(void);
};
void display()
{
cout<<”\n name:”<<name; cout<<”\n age:”<<age;
}
void main( )
{
person p;
p.getdata();
p.display();
}
INLINE FUNCTION: