CLASS AND OBJECT IN C++ WITH EXAMPLE
FOUNDATION
Lets define a variable x of integer type and give it a value 12. How will you do it. You will simply write
int x=12;
This statement can also be written as
int x;
x=12;
Here int is a primitive data type in C++ x is a variable of integer type and the statement x=12; assigns a value of 12 to the variable x of integer type
CLASS
A class can be defined in many ways as mentioned below
- A class is a user defined data structure.
2. A class is a programming construct.
3. In C++ Classes are defined as data structures that contain variables and functions.
The variables defined in the class are also called as data members or fields or variables. The functions defined in the class are also called as member functions or methods or functions.
Note: Functions are used to manipulate the value of the variables.
Classes enable you to create your own custom, self-contained, and reusable types.
A class file is considered a blueprint of objects that we use in our code.
We create class files to help model real-world objects in the code.
A class in C++ is a user defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers: private, protected or public (by default access to members of a class is private)
Class Members:
The functions and variables defined in the class are called Class Members.
“Consider a software that manages a bank ATM. To create the software a programmer needs to understand the concept of customers, accounts, transactions, etc. It's far easier to implement the software application by modeling these real world objects as software objects (classes) in your code.”
Creating Classes and Members
In C++, a class is a programming construct that you can use to define your custom types.
(A programming construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language. The term "language construct" is used as a synonym for control structure)
When you create a class, you are effectively creating a blueprint for the type. So a class is also called as Blue Print of an Object. The class defines the behaviors and characteristics of its objet.
Behaviors are represented by defining functions in the class whereas characteristics are represented by defining variables within the class.
Example:
Suppose you want to create a class to represent a rectangle shape in your program. You use the class keyword to declare a class, as shown in the following example:
//Declaring a Class
class Rectangle
{
public:
int width;
int height;
};
Here we have declared a class called Rectangle and given it two public member variables called width and height, that will be used to represent the width and height of our rectangle.
Note that they are accessible directly because they are public, as a result of the public: modifier.
Using a Class
We have created a class to represent a rectangle, now we use that class to create instances of a rectangle in our program.
When we create a new rectangle from this class, it is known as a rectangle object and will be given a unique name.
// How to use a Class
void main()
{
Rectangle Big;
Rectangle Small;
Big.width = 6
Big.height = 8;
Small.width = 2;
Small.height = 3;
}
Here we have created two rectangle objects and called them Big and Small.
Then, by using “dot notation" (or the dot operator), we have provided values for the width and height of each rectangle. The Big rectangle is 6 x 8 and the Small rectangle is 2x3.
Note: Initialization is an important part of working with your classes in C++.
Even when using intrinsic data types, if you do not initialize the variable for that type and you access it in your code, you will end up with whatever values are stored in the memory location that the variable refers to. This is something you want to avoid doing. You should always initialize your types in C++;
C++ offers a couple of options for initializing your classes. You can initialize the member variables by using the dot operator and setting the values explicitly or you can include a constructor in your class that is responsible for initialization the member variables.
EXAMPLE: CREATING A CLASS, OBJECT AND INSTANTIATION
class Student // Creating a class with name Student
{
public:
int age;
String name;
};
void main()
{
clrscr();
Student A; // A is an instance of the class Student
Student B; // B is also an instance of class Student
/*Class Student has two class members: age and name. Every object of class Student must have some specific values for these two class members. Assigning value to the class members for each object is called Initialization*/
// Initialization for object A of class Student
A.age=33;
A.name=”Ashish”;
// Initialization for object B of class Student
B.age=30;
B.name=”Parvati”;
getch();
}