1- In the world of object-oriented programming, we often want our types to not only hold data, but provide functions that work with the data as well. In C++, this is typically done via the class keyword. Using the class keyword defines a new user-defined type called a class. In C++, classes are very much like data-only structs, except that classes provide much more power and flexibility.
2- Functions defined inside of a class are called member functions or methods.
3- first we put the keyword “class” and “ClassName” and data members and function members of the class would be placed inside the brackets :
class ClassName
{
// data members and function members
};
4- A variable of a class type is called an object or an instance of a class.
Quiz)
1-
#include
using namespace std;
class IntPair
{
public:
int m_int1;
int m_int2;
void set(int int1,int int2)
{
m_int1=int1;
m_int2=int2;
}
void print()
{
cout<<"pair("<<m_int1<<","<<m_int2<<")"<<endl;
}
};
int main()
{
IntPair p1;
p1.set(1, 1); // set p1 values to (1, 1)
IntPair p2{ 2, 2 }; // initialize p2 values to (2, 2)
p1.print();
p2.print();
return 0;
}
2- Because we have to use member functions in order to set and print those variables ,so we define it as a class instead of a struct.