Module 1 C
Module 1 C
Module-1
Introduction to Object Oriented Programming
History of C++:
The C++ programming language was created by Bjarne Stroustrup (1980) and his
team at Bell Laboratories (AT&T, USA) to help implement simulation projects in an
object oriented and efficient way.
C++ can make use of existing C software libraries with major addition of “Class
Construct”.
This language was called “C with classes” and later in 1983, it was named “C++” by
Rick Mascitii. As the name C++ implies, C++ was derived from the C programming
language: ++ is the increment operator in C.
Both the languages share the same basic syntax. Also, almost all the operators and
keywords of C are present in C++ as well, and they do the same thing.
Similar notions of heap, stack, static, and file-scope variables are present in both of
these languages.
As compared to C, C++ has more extended grammar. But the basic grammar here is
the same.
No. C C++
C follows the procedural style C++ is multi-paradigm. It supports
1.
programming. both procedural and object oriented.
In C++, you can use modifiers for class
2. Data is less secured in C. members to make it inaccessible for outside
users.
Characteristics of C++:
Object-Oriented Programming
Is a Procedural programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating objects that
contain both data and functions.
OOPs characteristics:
Modularity: Module is a logically self-contained unit that can be tested and
executed independently.
Abstraction: Abstraction is the process of only showing the necessary details to the
user and hiding the other details in the background.
Data Encapsulation: Wrapping of data and functions into a single unit is called data
encapsulation.
Inheritance: The process by which objects of one class acquires the properties of
the objects of another class.
Polymorphism: The ability for a message to be processed in more than one form.
Dynamic Binding: Linking of a procedure call to the code to be executed when it is
called.
Message Passing: Message passing in C++ is the method of communication
between two or more objects. A message is used to communicate with other objects
by invoking the function of other objects.
Applications of OOP
Real-time systems
Simulation and modeling
Object-oriented databases
Hypertext, hypermedia and expertext
AI and expert systems
Neural networks and parallel programming.
Decision support and office automation systems
CIM/CAM/CAD systems
Class
A class is a way to bind the data and its associated functions together. It allows the
data (and functions) to be hidden, if necessary, from external use.
When defining a class, we are creating a new abstract data type that can be
treated like any other built-in data type. Generally, a class specification has two
parts:
1. Class declaration
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declaration;
};
The class declaration is similar to a struct declaration. The keyword class specifies,
that what follows is an abstract data of type class_name. The body of a class is enclosed
within braces and terminated by a semicolon. The class body contains the declaration of
variables and functions. These functions and variables are collectively called class
members. They are usually grouped under two sections, namely, private and public to
denote which of the members are private and which of them are public. The keywords
private and public are known as visibility labels. Note that these keywords are followed
by a colon.
The class members that have been declared as private can be accessed only from
within the class. On the other hand, public members can be accessed from outside the
class also. The data hiding (using private declaration) is the key feature of object-oriented
programming. The use of the keyword private is optional. By default, the members of a
class are private. If both the labels are missing, then, by default, all the members are
private. Such a class is completely hidden from the outside world and does not serve any
purpose.
The variables declared inside the class are known as data members and the
functions are known as member functions. Only the member functions can have access to
the private data members and private functions. However, the public members (both
functions and data) can be accessed from outside the class. This is illustrated in Fig. 1.1.
The binding of data and functions together into a single class-type variable is referred to
as encapsulation.
We usually give a class some meaningful name, such as item. This name now becomes
a new type identifier that can be used to declare instances of that class type. The class
item contains two data members and two function members. The data members are
private by default while both the functions are public by declaration. The function
getdata() can be used to assign values to the member variables number and cost, and
putdata() for displaying their values. These functions pro- vide the only access to the data
members from outside the class. This means that the data cannot be accessed by any
function that is not a member of the class item. Note that the functions are declared, not
defined. Actual function definitions will appear later in the program. The data members
are usually declared as private and the member functions as public. Figure 1.2 shows
two different notations used by the OOP analysts to represent a class.
Objects
Objects are the basic run-time entities in an object-oriented system. They may
represent a person, a place, a bank account, a table of data or any item that the
program must handle.
The fundamental idea behind object oriented approach is to combine both data and
function into a single unit and these units are called objects.
Creating Objects:
Remember that the declaration of item as shown above does not define any objects
of item but only specifies what they will contain. Once a class has been declared, we can
create variables of that type by using the class name (like any other built-in type variable).
For example,
creates a variable x of type item. In C++, the class variables are known as objects.
Therefore, x is called an object of type item. We may also declare more than one object in
one statement. Example: item x, y, z;
The declaration of an object is similar to that of a variable of any basic type. The
necessary memory space is allocated to an object at this stage. Note that class
specification, like a structure, provides only a template and does not create any memory
space for the objects.
Objects can also be created when a class is defined by placing their names
immediately after the closing brace, as we do in the case of structures.
That is to say, the definition
class item
{
.....
.....
.....
} x,y,z;
would create the objects x, y and z of type item. This practice is seldom followed
because we would like to declare the objects close to the place where they are used and
not at the time of class definition.
As pointed out earlier, the private data of a class can be accessed only through the
member functions of that class. The main() cannot contain statements that access
number and cost directly. The following is the format for calling a member function :
object-name.function-name (actual-arguments);
x.getdata(100,75.5);
is valid and assigns the value 100 to number and 75.5 to cost of the object x by
implementing the getdata() function. The assignments occur in the actual function. Please
refer Sec. 5.4 for further details.
Similarly, the statement
x.putdata();
would display the values of data members. Remember, a member function can be
invoked only by using an object (of the same class).
getdata(100,75.5);
has no meaning. Similarly, the statement
x.number = 100;
is also illegal. Although x is an object of the type item to which number belongs, the
number (declared private) can be accessed only through a member function and not by
the object directly.
It may be recalled that objects communicate by sending and receiving messages. This is
achieved through the member functions.
For example,
x.putdata();
sends a message to the object x requesting it to display its contents.
A variable declared as public can be accessed by the objects directly.
Example:
class xyz
{
int x;
int y;
public:
int z;
};
.....
.....
xyz p;
p.x = 0; // error, x is private
p.z = 10 // OK, z is public
.....
.....
The membership label class-name :: tells the compiler that the function function-
name belongs to the class class-name. That is, the scope of the function is restricted to the
class-name specified in the header line. The symbol :: is called the scope resolution
operator.
For instance, consider the member functions getdata() and putdata() as discussed
above.
Since these functions do not return any value, their return-type is void. Function
arguments are declared using the ANSI prototype.
The member functions have some special characteristics that are often used in the
program development.
These characteristics are:
Several different classes can use the same function name. The ‘membership label’
will resolve their scope.
Member functions can access the private data of the class. A nonmember
function cannot do so. (However, an exception to this rule is a friend function
discussed later.)
A member function can call another member function directly, without using the dot
operator.
class item
{
int number;
float cost;
public:
void getdata(int a, float b); // declaration
// inline function
void putdata(void) // definition inside the class
{
cout << number << “\n”;
cout << cost << “\n”;
}
};
Data Abstraction
Data Abstraction is a process of providing only the essential details to the outside
world and hiding the internal details, i.e., representing only the essential details in
the program.
Data Abstraction is a programming technique that depends on the separation of the
interface and implementation details of the program.
Let's take a real life example of AC, which can be turned ON or OFF, change the
temperature, change the mode, and other external components such as fan, swing.
But, we don't know the internal details of the AC, i.e., how it works internally. Thus,
we can say that AC seperates the implementation details from the external interface.
C++ provides a great level of abstraction. For example, pow() function is used to
calculate the power of a number without knowing the algorithm the function
follows.
Since the classes use the concept of data abstraction, they are known as Abstract
Data Types(ADT).
Encapsulation
The wrapping up of data and functions into a single unit (called class) is known
as encapsulation.
Data encapsulation is the most striking feature of a class. The data is not accessible
to the outside world, and only those functions which are wrapped in the class can
access it.
These functions pro- vide the interface between the object’s data and the program.
This insulation of the data from direct access by the program is called data hiding
or information hiding.
Inheritance
Inheritance is the process by which objects of one class acquire the properties of
objects of another class. It supports the concept of hierarchical classification. For example,
the bird ‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’. The
principle behind this sort of division is that each derived class shares common
characteristics with the class from which it is derived as illustrated in Fig. 1.3.
In OOP, the concept of inheritance provides the idea of reusability. This means
that we can add additional features to an existing class without modifying it.
This is possible by deriving a new class from the existing one. The new class will
have the combined features of both the classes.
The real appeal and power of the inheritance mechanism is that it allows the
programmer to reuse a class that is almost, but not exactly, what he wants, and to
tailor the class in such a way that it does not intro- duce any undesirable side-
effects into the rest of the classes.
Note that each sub-class defines only those features that are unique to it.
Without the use of classification, each class would have to explicitly include all of
its features.
Polymorphism
Message Passing
In OOP, message passing refers to communication between objects or processes
where data or signals are exchanged, often through method calls or events,
promoting a modular and flexible design.
It is a process by which objects communicate by exchanging messages, typically in
the form of method or function calls.
Example:
Objects have a life cycle. They can be created and destroyed. Communication with an object
is feasible as long as it is alive.
Example:
Programming Examples:
#include <iostream.h>
#include <conio.h>
void main()
average = sum/2;
getch();
#include<iostream.h>
#include<conio.h>
void main()
{
int a = 5, b = 10, temp;
clrscr();
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\n After swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
char c;
int a;
float b;
double d;
clrscr();
cout << "Size of char: " << sizeof(c) << " byte" << endl;
cout << "Size of int: " << sizeof(a) << " bytes" << endl;
cout << "Size of float: " << sizeof(b) << " bytes" << endl;
cout << "Size of double: " << sizeof(d) << " bytes" << endl;
getch();
4. Use of Class
#include <iostream.h>
#include <conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person :: getdata(void)
{
cout << “Enter name: ”;
cin >> name;
cout << “Enter age: ”;
cin >> age;
}
void person :: display(void)
{
cout << “\nName: ” << name;
cout << “\nAge: ” << age;
}
void main()
{
person p;
p.getdata();
p.display();
getch();
}