The most interesting feature in C++ is the ability to implement the Object Programming Concept. Well here I wont go into the theory of OOP or what are its details, I want to give an example of how to use class, how to create an object and how to invoke methods.
What are objects, object represent real life object, like a person. Well each person has different names say Mr. X, Mr. Y etc., but they all have some property that can be define by a class. We can define age, gender, name, address, etc for all. so such definition is a class. Lets take the following example:
#include<iostream> using namespace std; class Person //Defining the class Person { int age; char name[20], address[20]; public: void getInfo(); //public member methods void putInfo(); }; //defining method void Person::getInfo() { cout << "Enter your age: "; cin >> age; cout << "Enter Name: "; cin >> name; cout << "Enter Addres: "; cin >> address; } void Person::putInfo() { cout << "Name: " << name << endl; cout << "Address: " << address << endl; cout << "Age: " << age << endl; } int main() { Person A, B; //Creating two object of class Person A.getInfo(); // Accessing the methods B.getInfo(); A.putInfo(); B.putInfo(); return 0; }
From the above example we have defined a class Person which contain attribute age, name and address, then two public function (methods) are defined. Then from the main function, two object A and B are created which then access the methods using the dot operator. If you have question, spell it out in the comments thanks.