An object is an instance of a class, which represents a real-world entity that has attributes (data) and behaviors (methods or functions).
Objects are created from classes because they provide a way to create multiple instances of an entity that share the same attributes and behaviors defined by the class. For example, if we have a class named "Car" that defines the attributes and methods for a car, we can create multiple objects of that class, each representing a different car with its own set of attributes.
Objects are useful because they allow us to model real-world entities and interact with them in our programs. By creating objects from classes, we can encapsulate data and behavior, and provide a clear interface for working with the object. This can make our code more organized, easier to read and understand, and easier to maintain over time.
#include <iostream>
using namespace std;
class specName{
public:
void printSomething() {
cout << "Hack The Planet !" << endl;
}
};
int main() {
specName Object;
Object.printSomething();
specName ObjectNu2;
ObjectNu2.printSomething();
return 0;
}
An object of the class specName
is created twice in the main
function using the syntax specName Object;
and specName ObjectNu2;
. These objects are then used to call the printSomething()
method using the .
operator, which results in the output "Hack The Planet !" being printed twice in the console.
Dot notation is a way to access the members (variables and functions) of an object through the object name followed by a period (dot) and the name of the member.
#include <iostream>
using namespace std;
class specName{
public:
void printSomething() {
cout << "Hack The Planet !" << endl;
}
void test() {
cout << "Mess with the best, die like the rest." << endl;
}
};
int main() {
specName Object;
Object.printSomething();
Object.test();
return 0;
}
This code defines a class named specName
with two public member functions printSomething()
and test()
. The printSomething()
function prints the message "Hack The Planet!" to the console, and the test()
function prints the message "Mess with the best, die like the rest." to the console.
In the main()
function, an object of the specName
class is created using the specName Object;
syntax. The printSomething()
and test()
member functions of this object are then called using the dot notation, which is denoted by Object.printSomething();
and Object.test();
.
When the program is executed, it will print the messages "Hack The Planet!" and "Mess with the best, die like the rest." to the console.
#include <iostream>
using namespace std;
class specName{
public:
void printSomething() {
cout << "Hack The Planet !" << endl;
}
void test() {
cout << "Mess with the best, die like the rest." << endl;
}
};
int main() {
specName Obj3;
specName Obj4;
specName Obj5;
Obj3.printSomething();
Obj3.printSomething();
Obj3.printSomething();
Obj4.test();
Obj4.test();
Obj4.test();
Obj5.printSomething();
Obj5.printSomething();
Obj5.printSomething();
return 0;
}
This code creates three instances of the specName
class named Obj3
, Obj4
, and Obj5
.
The printSomething()
method is called three times on Obj3
, the test()
method is called three times on Obj4
, and printSomething()
is called three times on Obj5
.
This demonstrates that each instance of the specName
class is independent of the others and can have its own state and behavior.
No comments:
Post a Comment