Difference between Classes and Objects
What is Class?
A class in C++ is a blueprint for creating objects (data structures), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
A class definition begins with the word "class," then lists the class's name and body inside curly braces.The class body is where the member variables and member functions can be declared.
Syntax:
class ClassName {
public:
// member variables (also called data members or attributes)
data_type member_variable_1;
data_type member_variable_2;
...
// member functions (also called methods)
return_type member_function_1(parameters) {
// function body
}
return_type member_function_2(parameters) {
// function body
}
...
};
Explanation:
ClassName is the name of the class. The accessibility of the members of the class is determined by the access specifier public. Private members can only be accessed within the class, but public members can be accessed from anywhere in the application.
Data's type, or data type, can be an int, float, string, etc.member_variable_1, member_variable_2, etc. are the names of the member variables.The type of the value returned by the member function, such as int, void, etc., is known as return type.member_function_1, member_function_2, etc. are the names of the member functions. Parenthesis and commas separate the input parameters for the member function, which are denoted by parameters. If a member function does not take any input parameters, then the parentheses are left empty.The function body is enclosed in curly braces and contains the code that implements the behavior of the member function.
Example:
#include <bits/stdc++.h>
using namespace std;
class Rectangle
{
public:
int width, height;
voidset_values(int x, int y);
void area()
{
int ar = width * height;
cout<<"Area is: "<<ar<<endl;
}
};
void Rectangle::set_values(int x, int y)
{
width = x;
height = y;
}
int main()
{
Rectangle z;
z.set_values(10, 20);
z.area();
}
Output:
Area is: 200
Explanation:
In this example, the class Rectangle has two member variables width and height, and two member functions set_values and area. The set_values function sets the values of width and height, while the area function calculates and returns the area of the rectangle.
Access specifieer:
In object-oriented programming (OOP), access specifiers are keywords that specify the visibility and accessibility of class members like fields, methods, and constructors. These keywords control which parts of a class can be accessed by other objects, classes, and even outside of the program. There are three main access specifiers in OOP: public, private, and protected.
- Public: Anywhere in the program, whether in the same class, a subclass, or from a completely other class, can access members that have been designated as public.The main purpose of public access specifiers is to make members available to the rest of the program.
- Private: Only other members of the same class may access members that have been designated private.They are hidden from all other classes and objects and cannot be accessed directly from outside the class. This is useful for hiding implementation details that are not relevant to other parts of the program.
- Protected: Within the same class and its subclasses, members that have been designated as protected can be accessed. This allows the members to be accessed by subclasses, but not by other classes or objects outside of the class hierarchy. This is useful for creating a base class that can be extended by other classes while still maintaining control over the accessibility of its members.
In conclusion, access specifiers are an important aspect of OOP as they provide a means of controlling access to class members and maintaining the encapsulation and abstraction of a class. Public, private, and protected access specifiers each have their own use cases and provide different levels of visibility and accessibility for class members.
Example:
#include <iostream>
class Base {
public:
int public_field;
private:
int private_field;
protected:
int protected_field;
};
class Derived : public Base {
public:
voidaccess_fields() {
public_field = 1;
protected_field = 2;
// private_field = 3; // error: private_field is private
}
};
int main() {
Base base;
base.public_field = 1;
// base.private_field = 2; // error: private_field is private
// base.protected_field = 3; // error: protected_field is protected
Derived derived;
derived.public_field = 10;
// derived.private_field = 20; // error: private_field is private
derived.access_fields();
std::cout<< "Base public_field: " <<base.public_field<<std::endl;
// std::cout<< "Base private_field: " <<base.private_field<<std::endl; // error: private_field is private
// std::cout<< "Base protected_field: " <<base.protected_field<<std::endl; // error: protected_field is protected
std::cout<< "Derived public_field: " <<derived.public_field<<std::endl;
// std::cout<< "Derived private_field: " <<derived.private_field<<std::endl; // error: private_field is private
// std::cout<< "Derived protected_field: " <<derived.protected_field<<std::endl; // error: protected_field is protected
return 0;
}
Output:
Base public_field: 1
Derived public_field: 1
Explanation:
The program demonstrates the use of the access specifiers public, private, and protected in C++. The class Base defines three fields: public_field, private_field, and protected_field. Since the field public field is marked as public, anyone outside of the class can access it. Since the field private field is marked as private, only members of the class may access it. Since the field protected field is marked as protected, only members of the class and its subclasses can access it.
The class Derived is a subclass of Base and is declared as public. This means that it can access the public_field and protected_field of the Base class. In the method access_fields(), the public_field and protected_field can be accessed, but the private_field cannot be accessed as it is private.
In the main function, an instance of Base is created and the value of its public_field can be assigned and printed to the screen. However, the values of the private_field and protected_field cannot be assigned or printed to the screen as they are private and protected, respectively.
An instance of Derived is also created and its public_field can be assigned a value and printed on the screen. The access_fields() method is called on the Derived object, which sets the values of the public_field and protected_field. The values of the public_field and protected_field are then printed on the screen, demonstrating that they can be accessed from the Derived class.
What is an Object?
C++ is an object-oriented programming language that provides the features to create objects, encapsulate data and methods, and implement inheritance and polymorphism. In C++, an object is an instance of a class, which serves as a template for building objects. A class defines the properties and behaviors of objects of its type.
Objects are fundamental to object-oriented programming (OOP), which is a programming paradigm that focuses on the manipulation of objects rather than the management of code and data. Objects encapsulate data and behavior into a single entity, making it easier to understand and work with complex systems.
In C++, objects are created from classes. A class describes the traits and actions of an object's type and serves as a template for building new objects.Each object created from a class is unique, with its values for its member variables.
Objects can communicate with each other by sending messages to each other. In C++, this is done by calling the member functions of objects. A member function is a unique kind of function that is connected to a class and can be used with one of the class's objects.
Objects can also be used to model relationships between entities. For example, you can define a Student class and a Teacher class, and you can create objects of both classes to represent real-world students and teachers. The Student class could have a member function that returns the name of the teacher, and the Teacher class could have a member function that returns the names of the students in a class.
Inheritance is another key feature of objects in C++. You can build a new class from an existing class using inheritance.You can add more properties and behaviors to the new class as needed; it will inherit all of the previous class's attributes and behaviors. This allows you to create more specialized classes that are based on more general classes.
Polymorphism is another important feature of objects in C++. Different types of objects can be viewed as belonging to the same type thanks to polymorphism. This enables you to write code that is more flexible and reusable, as it can work with objects of different types in the same way.
In conclusion, objects are a crucial component of OOP and a strong tool for organizing and organizing your code.They encapsulate data and behavior, and they allow you to model real-world entities and relationships in your programs. With features like inheritance and polymorphism, objects provide a rich and flexible mechanism for writing high-quality, maintainable, and reusable code.
Example:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
void display() {
cout<< "Name: " << name <<endl;
cout<< "Age: " << age <<endl;
}
};
int main() {
Person person1;
person1.name = "John Doe";
person1.age = 30;
person1.display();
return 0;
}
Output:
Name: Joe Doe
Age: 30
Explanation:
In this example, a class named Person is defined, with name and age as its member variables and a display() member function. In the main function, an object named person1 is created from the Person class and its member variables are assigned values. The display() member function is called on the person1 object, which displays its properties.
This simple example only serves to show how to create an object in C++.In real-world programs, classes are usually more complex and have more member variables and member functions. Additionally, objects are often created dynamically, using the new operator, or they can be passed as arguments to functions or returned from functions.
Difference between Class and Objects
Classes
- Provide a blueprint or template for creating objects
- Define the attributes (data) and behaviors (methods) of objects
- Do not contain actual data, but describe what the data of objects will look like
- Are created at design time and exist in memory only as long as the program is running
- Can be reused to create multiple objects
Objects
- Represent a specific instance of a class
- Contain actual data, which is unique to each object
- Are created at runtime and exist in memory until they are destroyed or the program terminates
- Follow the blueprint defined by the class to have specific attributes and behaviors
- Can interact with other objects and respond to messages or events
The use of classes in object-oriented programming (OOP) allows for the formal modeling of real-world notions. By defining the attributes and behaviors of objects, classes provide a blueprint for creating objects that have a consistent structure and behavior. This helps to reduce the complexity of the code and makes it easier to manage and maintain the code over time.
On the other hand, objects are runtime-generated instances of a class. Objects contain the data that is specific to that instance of the class, and they exhibit the behaviors that are defined in the class. When a program is running, multiple objects can be created from the same class, each with its unique data.
For example, consider a class called "Person". The Person class might have attributes such as name, age, and address. The Person class might also have behaviors such as introducing, speaking, and listening. Each instance of the Person class, or each Person object, would have its specific values for name, age, and address, but would exhibit the same introduce, speak, and listen to behaviors as every other Person object.
In conclusion, the relationship between classes and objects is an important aspect of OOP. Classes provide a blueprint for creating objects, while objects are instances of classes that contain specific data. The code is made simpler by this structure, which also makes it simpler to manage and maintain over time.