An introduction of Object-oriented
Programming concepts in C++ with example
Fundamental terms and
basic idea
Everything in oops is associate with class and object. The class also have an attribute (Variable) and methods (Function). An object is known as a model
of some real or imaginary world. For example, your laptop is an object. The
laptop has attributes, such as color, weight, ram, and method, such as shutdown.
Example
Object |
Properties |
abilities |
Ram |
Hair color:black height:180 cm |
Walk,speak,see,hear |
Shyam |
Hair color: green height:140 |
Walk,speak,see,hear |
Laptop |
Company :Dell |
Power on,boot,crash |
Pendrive |
Capacity:16 GB |
Read, write |
Class: Group of
object with similar properties and common behavior.
A class is a user-defined data type the same as int or float. The class used in our program as a data type. We can also say it’s a blueprint to creating
object.
Example
Class |
Properties |
abilities |
Person |
Name:string,hair colour:string, height:integer |
Walk,speak,see,hear |
Computer |
Type:string, capacity:integer |
Power on,boot,crash |
The properties of
class is called attributes and abilities are called method.
Data encapsulation
Normally the internal arrangement of the class should not be
visible form the external class. To access restricted attribute of an internal class public
keyword is used. There are private and protected are also used in
class.
Inheritance
Assume you create a class of Ram. This class has attributes
like Hair color and height and method walk, speak. If you have to take another
class shayam .Shayam class also have the same attribute and method, If you use inheritance
you can inherit rams attribute and method directly without writing.
Polymorphism
A call of the method may invoke different codes.Function with
same name but different data type or passing value.
Class Diagram
Call diagram is consist of class name, attributes and
methods and their operations.
Access Specifier in
C++
· + : public (No access restriction) the attribute can access form outside class
· -: private (Access only
inside the class) attribute or method who is private can only access within
the class. That is a reason public function is using to set value in the attribute.
· #: Protected (Very useful in inheritance)Access where object is visible.
Note
By default, all members declared with in-class has private access specifiers in C++.
Inheritance
This is most useful concept in OOPs, this allows building hierarchy
of classes. Exam ram and shayam are specializations are human, We can say human
is the superclass of ram. Ram is a subclass of human.
A subclass inherits all attributes and operation of their superclass.
We are free to add some extra attributes and methods in the subclass. To subclass
have their own attribute and method as well as Superclass attributes and
methods.
0 Comments
If you have any doubt and suggestion Please let me know