Hello friends, in this post today, we will learn about what is class in c++ and how it is accomplish in c++. so let's start....

What is class

1. introduction to class:-
- A class is a template that specifies the attributes and behavior of things or object
- Class means to achieve encapsulation.
- A class is a user defined new data type like Student, Book, Account detail etc.
- A class is a blueprint or prototype for the objects which are created.
- A class is an implementation of an abstract data type (ADT).
- It defines attributes and methods.
- Class is a description of object’s property set and set of operations.
- For creating a new class we use class keyword.
- The body of class must be enclosed within curly braces({}) and terminated by a semicolon(;).
2. some real life examples:-
1. Animal can be stated as a class and Dog, Python, Deer, Monkey, Cow, etc., are
its object.
2. Person can be stated as class and Ram, Rahul, Teacher, Student etc., are its objects.
3. Musician can be stated
as a class and Arman malik, Anu Malik, Sonu nigam are its objects.
3. syntax of class:-
4. Accessing class member:-
- Private members of the class can only accessed by the members functions with in that class.
- The class members ( functions or data), which are declared as public can be accessed from outside the class
- The class members, which are declared as Protected can be accessed within the class and from derived class but cannot be accessed from any other class.
- public, private, and public these are access specifiers or visibility labels that control the flow of data member and function outside the class.
- Remember:-If we not declare any visibility mode in our program, all data members and function become private.
- If you are accessing class member outside class can be done by using dot operator (.) and object of that class using following syntax,
- object-name.function-name(actual-arguments);
Output:-
5. Explanation of above example:-
- smallobj is the name of class.
- somedata is a data member of the class type smallobj.
- setdata and showdata are the member functions of the class type smallobj.
- s1 and s2 are the object of smallobj.
6. Defining member Functions:-
- Member functions of a class can be defined two ways:-
- The function definition in both class would be identical except for the way in which the header is defined.
6.1 Inside the class definition:-
- Member function can be define inside the class as illustrated in example. where both the functions are defined inside the class.
- A member function defined inside a class is treated as an inline function. There, only small function must be defined within the class definition.
Output:-
6.2 Outside the class definition:-
- when prototype of member functions are declared inside a class, they must be defined separately outside the class with the help of scope resolution operator represented as double colon ( :: ).
#include<iostream>using namespace std;class complex{ private: int a,b;
public: void setdata(int, int); void showdata();};
void complex::setdata(int x ,int y){ a=x; b=y;}
void complex::showdata(){ cout<<"a="<<a<<endl<<"b="<<b;}
int main(){ complex c1; //objects of class c1.setdata(10,20); //call member function c1.showdata(); //call member function return 0;}
Output:-
7. some special characteristics of member functions are:
- The same function name can be used in several different classes, since the membership label will identify the class to which they belong.
- Member function are allowed to access private data of that class.
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Nice good job
ReplyDelete