Академический Документы
Профессиональный Документы
Культура Документы
There are a few principle concepts that form the foundation of object-
oriented programming:
Object
This is the basic unit of object oriented programming. That is both data
and function that operate on data are bundled as a unit called as object.
Class
When you define a class, you define a blueprint for an object. This
doesn't actually define any data, but it does define what the class name
means, that is, what an object of the class will consist of and what
operations can be performed on such an object.
Abstraction
Data abstraction refers to, providing only essential information to the
outside world and hiding their background details, i.e., to represent the
needed information in program without presenting the details.
Encapsulation
Encapsulation is placing the data and the functions that work on that
data in the same place. While working with procedural languages, it is
not always clear which functions work on which variables but object-
oriented programming provides you framework to place the data and the
relevant functions together in the same object.
Inheritance
One of the most useful aspects of object-oriented programming is code
reusability. As the name suggests Inheritance is the process of forming a
new class from an existing class that is from the existing class called as
base class, new class is formed called as derived class.
Polymorphism
The ability to use an operator or function in different ways in other words
giving different meaning or functions to the operators or functions is
called polymorphism. Poly refers to many. That is a single function or an
operator functioning in many ways different upon the usage is called
polymorphism.
3) Friend function
A friend function of a class is defined outside that class' scope but it has
the right to access all private and protected members of the class. Even
though the prototypes for friend functions appear in the class definition,
friends are not member functions.
4) Constructer
A constructor will have exact same name as the class and it does not
have any return type at all, not even void. Constructors can be very
useful for setting initial values for certain member variables.
5) Parameterized constructer
Parameterized Constructor
A default constructor does not have any parameter, but if you need, a
constructor can have parameters. This helps you to assign initial value to
an object at the time of its creation.
6) Copy Constructer
7) Importance of destructer
A destructor will have exact same name as the class prefixed with a tilde
(~) and it can neither return a value nor can it take any parameters.
Destructor can be very useful for releasing resources before coming out
of the program like closing files, releasing memories etc.
In C++ a constructor is used for initializing an object. Usually, you would use a
constructor to pass arguments into a newly instantiated object, and those arguments
would be set to private members of the object. You can also use a constructor to
perform class related logic at the time of instantiation.
A destructor is called after the parent object is removed from scope or when it is
deleted from memory. Usually, you would use a destructor to deallocate dynamically
allocated class members from memory, or you may use it perform any other logic
before the object is removed from memory.
When declaring a class in C++ you do NOT have to explicitly define a constructor or
destructor for your class because they are already implicitly defined.
C++ allows you to specify more than one definition for a function name
or an operator in the same scope, which is called function
overloading and operator overloading respectively.
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
void print(double f) {
void print(char* c) {
};
int main(void) {
printData pd;
pd.print(5);
pd.print("Hello C++");
return 0;
declares the addition operator that can be used to add two Box objects
and returns final Box object. Most overloaded operators may be defined
as ordinary non-member functions or as class member functions. In case
we define above function as non-member function of a class then we
would have to pass two arguments for each operand as follows:
#include <iostream>
class Box {
public:
double getVolume(void) {
length = len;
}
breadth = bre;
}
height = hei;
}
private:
double length; // Length of a box
};
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// volume of box 3
volume = Box3.getVolume();
When the above code is compiled and executed, it produces the following
result:
When the above code is compiled and executed, it produces the following
result:
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
declares the addition operator that can be used to add two Box objects
and returns final Box object. Most overloaded operators may be defined
as ordinary non-member functions or as class member functions. In case
we define above function as non-member function of a class then we
would have to pass two arguments for each operand as follows:
#include <iostream>
class Box {
public:
double getVolume(void) {
Box box;
box.length = this->length + b.length;
return box;
}
private:
int main( ) {
Box Box1; // Declare Box1 of type Box
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// volume of box 3
volume = Box3.getVolume();
return 0;
}
When the above code is compiled and executed, it produces the following
result: