Classes & Objects
C++ is an object-oriented programming language, which means it is built around the concept of objects. An object is an instance of a class, and a class is a user-defined data type that encapsulates data and functions. In other words, a class is a blueprint for creating objects.
To create a class in C++, you use the keyword "class" followed by the class name and the class body, which contains data members and member functions. Here is the general syntax for declaring a class in C++:
copy
class class_name {
private:
// data members and functions
public:
// data members and functions
protected:
// data members and functions
};
The access specifiers "private", "public", and "protected" determine the visibility of the data members and member functions. Private members can only be accessed by other members of the class, while public members can be accessed from anywhere in the program. Protected members are similar to private members but can be accessed by derived classes.
When you create an object of a class, you are instantiating that class, which means you are creating a copy of the class with its own set of data members and member functions. To create an object of a class, you use the class name followed by parentheses, like this:
copy
class_name object_name;
You can then access the data members and member functions of the object using the dot (.) operator. For example, if you have a class called "Person" with a data member called "name" and a member function called "printName", you can create an object of the class and call the member function like this:
copy
Person john;
john.name = "John Smith";
john.printName();
Member functions are functions that are defined inside the class body and can access the data members of the class. They are also known as methods. Data members are variables that are declared inside the class body and can be accessed by the member functions.
In general, it is good practice to make data members private and provide public member functions to access and modify them. This is called encapsulation and helps to protect the data from being accessed or modified in unintended ways.
Here is an example of a simple class declaration for an employee
copy
#include <iostream>
#include <cstring>
using namespace std;
class employee {
char name[80]; // private by default
public:
void putname(char *n); // these are public
void getname(char *n);
private:
double wage; // now, private again
public:
void putwage(double w); // back to public
double getwage();
};
void employee::putname(char *n)
{
strcpy(name, n);
}
void employee::getname(char *n)
{
strcpy(n, name);
}
void employee::putwage(double w)
{
wage = w;
}
double employee::getwage()
{
return wage;
}
int main()
{
employee ted;
char name[80];
ted.putname("Ted Jones");
ted.putwage(75000);
ted.getname(name);
cout << name << " makes $";
cout << ted.getwage() << " per year.";
return 0;
}
In this example, the "employee" class is used to store an employee's name and wage. Notice that the public access specifier is used twice. Although you may use the access specifiers as often as you like within a class declaration, most programmers find it easier to have only one private, protected, and public section within each class.
Finally, it's important to note a few restrictions that apply to class members. A non-static member variable cannot have an initializer. No member can be an object of the class that is being declared. No member can be declared as auto,