README.md

December 5, 2022 ยท View on GitHub

C++ Black Book, Chapter 6 (Objects and Classes)

Description

If you already know the basics of C++, here is where to begin...

More Info

Submitted On
ByD/D++
LevelIntermediate
User Rating3.8 (15 globes from 4 users)
CompatibilityC++ (general), Microsoft Visual C++, Borland C++
CategoryMiscellaneous
WorldC / C++
Archive File

Source Code

Objects and Classes

So far we know that structures are a way to put data elements together and functions organize the functionality of the program together. Now we will put both data elements and functions together with the use of classes.

Lets say you are a scientist who wants to program a robot. This robot must have some characteristics and some functionality, so how do you give it one?

The first thing we should do is creating a Class. A class has two parts:

1. Attributes (Characteristics)
2. Behaviors (Functionality)

In the attributes section you have to put your data members. In the behaviors section you have to put your member functions. Now we have a Robot.

In order to create several of these robots, we need to create them in the form of objects. So each robot becomes an object and will carry similar attributes and behaviors. Also lets say one of the attributes of our robot is giving it a name and an age. This enables them to have different names and ages.

So here is how the class will look like:

class Robot
{
private: char name[30]; int age;
public: Show_name(); void Set_info();
Robot(); /*this is called a constructor, it's job is to initialize data members and must have the same name as your class, I will talk about constructors in chapter 7*/
}

//note that we are not passing any arguments to our functions, this is because the functions show_name() and Set_info() have direct access to our data members and are the members of the same class.
Public means accessible from anywhere. /*also note that we set our data members as private, this is because we don't want any thing out side the class access them directly to make any changes to them. The only way we can change our data members is by using what we call accessor functions (like Show_name(); Set_info();) to modify or read our data members.*/

And this is how we implement our functions:

Robot::Robot() //initializing our data members
{
strcpy(name," ");
age=0;
}

void Robot::Set_info() /*:: this is a "Scope-resolution" which means that this function is from Robot class not any other class. Here we are modifying our data members*/
{
cout<< "Enter name";
cin>>name;
cout<< "Enter age";
cin>>age;
}

Robot::Show_name()
{
return name;
}

Now in main() we have to create our objects, so we say:

void main()
{
Robot MachineRobot,CrappyRobot; //creating two objects of type Robot /*Note that we have used "Robot" to indicate that we are creating objects of this class type. */
MachineRobot.Set_info(); //Get info for MachineRobot CrappyRobot.Set_info(); //Get info for CrappyRobot
MachineRobot.Show_name(); //Display the name of this robot
CrappyRobot.Show_name(); //Display the name of this robot
}

So we used the accessor functions that we made to give our robots a name and an age. The output of the program will be:

Enter name: FirstRobot
Enter age: 25
Enter name: SecondRobot
Enter age: 12
FirstRobot SecondRobot

To summerize:

We created a class that included certain attributes and certain behaviors. Then we created two object that carry with them the attributes and behaviors of that.

P.S You are welcome to ask any questions.

Pooya K.

Go ahead an search Google, the only place you will find my tutorial is on this site