What Is A Class??
A Class is a User-defined data type that resembles a structure . A Class can have data Members, but unlike the structure we have seen thus far, classes can also have Member Functions. The data Members can be of any type, Whether defined by the language or by us. The Member Function can Manipulate the data, create and destroy class variables, and even redefine C++'s Operators to act on the Class Objects.
Up to this point, we have been writing programs that did not use any of C++'s Object-Oriented Capabilities. Thus, the program in the preceding modules reflected structured programming, not Object-Oriented-Programming. To Write Object-Oriented Programs, we will need to use classes. The Class in C++'s basic unit of encapsulation. Classes are used to create objects. Classes and Objects are so fundamental to C++ that much of the remainder of these tutorial is devoted to them in one way or another.
In above Figure, We just create an Laptop Class. this class have some properties like Display, Hard disk, Processor, RAM, color, OS. and some Methods like ON, OFF, Processing, video, Audio, Internet. Then we create some more Object from Laptop Class, and each Laptop have same Methods, but different Properties. we can say that, we just create an Laptop Sketch, and using this sketch, we create some more. Each Laptop have Got some Different Properties , like It's have different Display, the color could be black or white, and RAM could be different. But we can turn on these all Laptops and turn off again, we can use internet using all these laptops or watch some videos. The Methods will be same.
In C++ these Properties are known as variable, which will be different for all Object. And Methods are known as Function. Which could be same for all variables.
Class Fundamentals:
Let's begin by reviewing the terms class and object. A Class is a template that defines the form of an object. A Class specifies both code and data. C++ uses a class specification to construct objects. Objects are instances of a Class. Thus, a class is essentially a set of plans that specify how to build an object. it is important to be clear on one issue: A Class is a logical abstraction. it is not until an object of that class has been created that a physical representation of that class exists in Memory.
When We define a Class, We declare the data that it contains and the code that operates on that data. while very simple classes might contain only code or only data, most real-world classes contain both. Data is contained in instance variables defined by the class, and a code is contained in functions. The code and data that constitute a class are called members of the class.
“
Most C++ Compilers contain many built-in Classes that are ready for us to use. For example: in String Tutorial, we were introduced to the built-in C++ " String " class. Using this class makes it easier for us to handle string in our Programs.
Classes and Objects:
The classes is the sketch or Blueprint, same like Laptop Class with some properties and some Methods or functions. So using this Laptop Class, we can create more Laptops known as Object Having same Mathods but different properties. Classes are the building block of Object Oriented programming.
Defining Classes:
When we define a class, we define a blueprint for a data type. it is a New data type we create that is more complex than the basic data types. Classes provide a description of an object. they detail all the data an object has and all the actions the object can perform. Additionally, classes provide a convenient way to group related data and the functions that use the data. One advantage of creating a class is that when we create an object form the class, we automatically create all the related fields. Another advantage is that we gain the ability to pass an object into a function, or receive an object from a function as a returned value, and automatically pass or receive all the individual fields that each object contains. Another advantage to using classes and object is that we think about them and manipulate them similarly to the way we use real-life classes and objects.
A Class definition starts with the keyword CLASS followed by the class name; and the class body, enclosed by a pair of curly braces. A Class definition must be followed either by a semicolon or a list of declarations. The basic syntax for class is:
class class_name
{
//Class Variables or Properties
Access-specifier:
Data-Type x;
Data-Type y;
//Class Methods or Functions:
Access-specifier:
Return-Type function_name()
{
//Function Body:
}
}
Let's create a simple class to know how to create a class and how to use it.
class Message
{
//Private Members:
private:
string name = "Omar";
int age = 22;
float marks = 95.65;
//Public Members:
public:
void display() //Function to display message:
{
cout<<name<<" is "<<age<<" year Old: "<<endl;
cout<<"He got "<<marks<<"% marks in his exam: ";
}
};
Above, we create a simple class Message to display message on screen. We declare Class properties/variables as private member, and method/function as public member. No one can access the class properties from outside the class. Only class member can access the properties. but we can access method/function from outside the class.
Using a Class:
After Declaring the Class, we can use the class Methods and properties using class objects. Let't take an simple real example to understand the classes and it's object. Now a day's Facebook is Most Popular Social website in the World, so let's take Facebook as an our Example:
Let's suppose the Facebook is an Class. so there are around Million User Account in Facebook Data center. it's Mean there are Million object of Facebook-Class. when ever a New person open it's New account then the Facebook-class just create an New object. there are some properties ( Like user-name , Profile-picture, timeline-cover, it's update, etc.) and Methods ( login, logout, update-status, upload picture or video, etc. ) in Facebook-class. so whenever somebody will create New Account, Facebook-class will create New Objects with it's properties, and Methods.
The Properties might be change (Example: all Facebook-User have different Profile picture, different timeline cover, different status, different info, etc.) but Methods will be same,(Example: all user can login, logout, can change their picture, or there status etc.) so this is the very simple example to know the real meaning of class and it's object.
It's enough Introducation of Classes and Object. now let's see, how we can create object. and how to use it.
class Message
{
//Class Members:
};
main()
{
//Creating Object:
Message msg;
}
Above, There is simple way to create an object of the class. we create the class Message and in our Main() method, we create object of that class msg. We create the Class, and then object. and now it's time to use the class. let's see how we can use the class member in our main() method.
/* Example - Classes & Objects- InfoBrother:*/
#include<iostream>
using namespace std;
class Message
{
private:
//Private properties:
string name = "Omar";
int age = 22;
float marks = 95.65;
public:
//Public method to display message;
void display()
{
cout<<name<<" is "<<age<<" year Old: "<<endl;
cout<<"He got "<<marks<<"% marks in his exam: ";
}
};
main()
{
//Creating Object:
Message msg;
//Calling Class Public Member using Dot Operator:
msg.display();
return 0;
}
Class & Object:
Omar is 22 year Old:
He got 95.65% marks in his exam:
Above there is an complete and easy example to understand class. We create a Class, and in main() method, we declare its object and using Dot (.) Operator, we call the public member of class in our main()program:
Creating Object:
After Declaration class, we can create object as much as we want. We can create single object of class as shown in the given example:
class Class-Name
{
//Class Members:
};
main()
{
//Creating object:
Class-Name Class-Object;
}
Or we can create Multiple object of single class, as shown in the given example:
class Class-Name
{
//Class Members:
};
main()
{
//Creating Multiple object:
Class-Name Class-Object1, Class-Object2, Class-Object3, Class-Object4;
}
Defining Class Methods:
We can define class method/function inside the class as shown in above class examples. but we can define function definition outside the class. Defining the Member function within the class definition declares the function inline, even if we do not use the inline specifier. if we want to define the function definition outside the class, then we need to declare function prototype inside the class public members, and definition outside of the class using Scope Resolution Operator ( ::). as shown in the given example:
class Message
{
public:
//Public method prototype to display message.
void display();
};
//Public method Definition to display message.
void Message::display()
{
//function body:
}
main()
{
//Creating Object:
Message msg;
//Calling Class Public Member using Dot Operator:
msg.display();
return 0;
}
In this tutorial, we discuss Class and its object. The class is the building block of Object-oriented programming. In our next tutorials, we will discuss more class and its members.