Classes and objects in C++.
Object Oriented Programming:
Four Principles:
The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism. These words may sound scary for a junior developer.
Object Oriented Programming is a concept that is based on real world objects.
What is an object? An object is any independent unit. In object oriented programming you have to think more closer about the real world, as it is related to objects. An object can be a book,pen and person etc. A person is also an object. Every object has it's own unique identity,just as objects in the real world do.
It is possible that two objects look identical, but they are still separate, unique objects.
What are attributes? Object's also have characteristics that are used to describe them. For example: A car can be blue or black, a glass can be full or empty. Object's have multiple attributes.
What are methods? Method describes the behavior of our object. Method is another name of function,that belongs to a class. But when we use the word method we consider it as the function of our object.
Let us consider an object cat. What will be cat's method's? Cat eats is a method. Cat drinks is also a method that describes the behavior of our object cat.
Object's are not always physical items. object can be a date, time and a bank account. You can't see or touch a bank account, but still it is a well defined object. It has it's own identity, attribute and behavior.
Each object has it's own identity and attribute. Each exhibits its own behavior ( Method ).
What is a Class? Objects are created using classes. A class can be described as an objects blueprint.
#include <iostream>
using namespace std;// class keyword is used to define a class.
Here Student is the class name.
class Student{
// public attributes
public:
string name;
int age;
int clas;
public:
// Method that shows data.
void showData(){
cout<<"Name is:"<<name<<endl<<"Age is:"<<age<<endl<<"Class in which studying:"<<clas<<endl;
}
};
//Once we have written the class, we can move on to create objects
that are based on that class.
Each object is called an instance of a class.
The process of creating objects is called instantiation.
Objects of the class are created in main method.
int main(){
// creating objects of our class Student.
Student s1;
Student s2;
Student s3;
s1.name="Humna";
s2.name="Bushra";
s3.name="Amna";
s1.age=13;
s2.age=17;
s3.age=18;
s1.clas=12; // we cannot use class keyword as attribute.
s2.clas=13;
s3.clas=14;
s1.showData();
s2.showData();
s3.showData();
return 0;
}
Output:
Name is:Humna
Age is:13
Class in which studying:12
Name is:Bushra
Age is:17
Class in which studying:13
Name is:Amna
Age is:18
Class in which studying:14What is the purpose of using OOP:
Most of the beginners get confused about the concept of OOP. Too many confusions come
in their mind.Importance of OOP is as follows:
It is good for defining abstract data types.
Implementation details are hidden from other modules and other modules has a clearly defined interface.
It is easy to maintain and modify existing code as new objects can be created with small differences to existing ones.It can be difficult at the beginning,but once you have understood all the concepts then everything will make sense.
Keep going with this work ☺️💯
ReplyDeleteWish you a best of luck for more...
I have read this. This is veey helpful, Thanks.
ReplyDelete