What is Class & Object in OOP |Difference Between Structure and Class | OOP Tutorial in C++ -03
What is Class in OOP?
Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. It is a logical entity.
Collection of objects is called class. It can have fields, methods, constructors etc.
The variables inside class definition are called as data members and the functions are called member functions.
Below is the syntax of class definition:
class ClassName{
Access specifier:
Data members;
Member Functions()
{
// member function defintion
}
};
class ClassName{ Access specifier: Data members; Member Functions() { // member function defintion }
};
Let's see an example of C++ class:
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute,field or data member
string myString; // Attribute,field or data member
};
class MyClass { // The class public: // Access specifier int myNum; // Attribute,field or data member string myString; // Attribute,field or data member };
What is Object in OOP?
Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
Each object has different data variables. Objects are initialized using special class functions called Constructors. And whenever the object is out of its scope, another special class member function called Destructor.
To create an object of Blog, specify the class name, followed by the object name. To access the class attributes (blogNo and blogName), use the dot syntax (.) on the object:
Create an object called "Blog" and access the attributes:
Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.
Each object has different data variables. Objects are initialized using special class functions called Constructors. And whenever the object is out of its scope, another special class member function called Destructor.
To create an object of Blog, specify the class name, followed by the object name. To access the class attributes (blogNo and blogName), use the dot syntax (.) on the object:
Create an object called "Blog" and access the attributes:
class Blog{ // The class
public: // Access specifier
int blogNo; // Attribute (int variable)
string blogTitle; // Attribute (string variable)
};
int main() {
Blog CodeShikhi; // Create an object of MyClass
// Access attributes and set values
CodeShikhi.blogNo = 03;
CodeShikhi.blogTitle = "Class and Object in C++";
// Print attribute values
cout << CodeShikhi.blogNo << "\n";
cout << CodeShikhi.blogTitle;
return 0;
}
Output:
03Class and Object in C++
Difference Between Structure and Class :
Structure
Class
StructureIf access specifier is not declared explicitly, then by default access specifier will be public.
ClassIf access specifier is not declared explicitly, then by default access specifier will be private.
StructureSyntax of Structure: struct structure_name { // body of the structure. }
ClassSyntax of Class: class class_name { // body of the class. }
StructureThe instance of the structure is known as "Structure variable".
ClassThe instance of the class is known as "Object of the class".
class Blog{ // The class public: // Access specifier int blogNo; // Attribute (int variable) string blogTitle; // Attribute (string variable) }; int main() { Blog CodeShikhi; // Create an object of MyClass // Access attributes and set values CodeShikhi.blogNo = 03; CodeShikhi.blogTitle = "Class and Object in C++"; // Print attribute values cout << CodeShikhi.blogNo << "\n"; cout << CodeShikhi.blogTitle; return 0; }
Structure | Class |
---|---|
StructureIf access specifier is not declared explicitly, then by default access specifier will be public. | ClassIf access specifier is not declared explicitly, then by default access specifier will be private. |
StructureSyntax of Structure: struct structure_name { // body of the structure. } | ClassSyntax of Class: class class_name { // body of the class. } |
StructureThe instance of the structure is known as "Structure variable". | ClassThe instance of the class is known as "Object of the class". |
0 Response to What is Class & Object in OOP ~ Difference Between Structure and Class -03
Post a Comment