How to Create Class in C ++: Through a class, we logically describe a Real World Object in the computer as a new Data Type and create new Data Type Objects based on that description and process it in the same way. Let's do, the way the process is done in the Real World with those objects.
The biggest feature of "C ++" is it's new concept of Class and Objects . Class is actually an extension of the Structure of "C Language". The class Keyword is used to define a class . The format for defining the class is as follows-
class class_name
{
private:
Data_Members;
Member_Functions;
public:
Data_Members;
Member_Functions;
};
The way we use Structure to Define New Data Type , similarly the class is also used to create New Data Type. But while to declare Variables of Structure type of Data Type, we have to use struct Key word with the name of Structure, whereas to declare Variables of Data type of class type, we have to classify Keyword with class name. Have not to use. Also Variables of type Class are identified by the name of Object instead of Variables.
The members of the structure are Global or Public by default while the members of the class are Local or Private by default. The other difference between class and Structure is that in Structures, the data of Structure can be used anywhere in the function program, while the Functions of using the data members of the class are Declare within the class and we only use these Member Function. The data members of the class can be used by.
Data Members and Member Functions that are written in a private area in a class, only those Data Members and Member Functions can be accessed by that class. Whereas Data Members and Member Functions which are written in public area, those Data Members and Member Functions can be used anywhere in the entire program. Just as Members of Structure can be used anywhere in the program.
To understand the class well, we will first convert it to a class by modifying a structure and then use it to understand the characteristics of the class in order.
struct book_bank
{
char title[20];
char author[30];
int page;
float price;
} book1;
A structure has been created here which has a Variable book1. If we want to display the title of a book, the name of the author, the number of pages of the book and the price of the book by displaying it, then we have to write a main () function like the following-
#include <iostream.h>
#include <conio.h>
struct book_bank
{
char title[20];
char author[30];
int page;
float price;
void Input(void)
{
cout << "Enter Book Title t ";
cin >> title;
cout << "nEnter Book Author t ";
cin >> author;
cout << "nEnter Book Page t ";
cin >> page;
cout << "nEnter Book Price t ";
cin >> price
}
void Display(void)
{
cout<< "Book Name t :" << title;
cout<< "nBook Author t :" << author;
cout<< "nBook Page t :" << page;
cout<< "nBook Price t :" << price;
}
};
void main(void)
{
book_bank book1;
clrscr();
book1.Input();
book1.Display();
getch();
}
By looking at these programs, you might be confused about how this structure is used. Therefore, first we understand this program.
We know that a structure is a group of logically related data of different types of data types. That is, we can place data of different types in a structure. When we can put members of different types of data in a structure, we can also put different types of functions in the same structure and the way we can declare variables of a structure type, we can, by those variables, different members of the structure We can access these functions using Dot Operator or Arrow Operator in the same way.
Suppose we have to name a Book in this Structure, then we declare a Structure type Variable of type book_bank. We can do this by the following statement: -
book_bank book;
Using the Dot Operator with the same Structure Variable " book ", we have been able to store a name as follows in the Member title located in Structure : -
From this statement, "Ramayan" Store becomes a member of the title of Structure. By adopting the same procedure, we can also access the Declare and Defined function in a structure using Dot Operator.
In the Structure of the above program, we have written two Functions as Members of the Structure, along with define them. Then we have done a Structure type Variable Declare named book1 and called a Member of Structure book_bank which is a Function by book1.Input () statement.
This Statement Compiler has Define these states that a Function Name Input, which book_bank Structure and the Function that Structure of Member Function, so it is Dot being Use the Operator.
As soon as the program is run , after Variable Declare, Program Control executes the book1.Input () statement directly, then Program Control directly reaches the Structure and starts executing the Statements of the Input () function. gives.
This function is in a Statement Block {} , and this is where all the members are Declare, so the function does not have to tell that we are assigning values to the members of a structure, since the function itself is in the same structure. The Input () Function gets the values and puts them in the Structure and the Display () Function prints the Store values in the Structure.
#include <iostream.h>
#include <conio.h>
class book_bank
{
public:
char title[20];
char author[30];
int page;
float price;
void Input(void)
{
cout<< "Enter Book's Title t";
cin>>title;
cout<< "nEnter Book's Author t";
cin>>author;
cout<< "nEnter Book's Page t";
cin>>page;
cout<< "nEnter Book's Price t";
cin>>price;
}
void Display(void)
{
cout<< "Book Name t :" << title;
cout<< "nBook Author t :" << author;
cout<< "nBook Page t :" << page;
cout<< "nBook Price t :" << price;
}
};
void main(void)
{
book_bank book1;
clrscr();
book1.Input();
book1.Display();
getch();
}
The way we can directly access the members of a structure, similarly we can also use the members Declared in the public area in a class . For example, if we want to store the name and price of a book in this class, we can do this according to the following statement-
book1.title = “Ramayana”;
book1.price = 1230;
Generally, the Declarations done inside the class are written in the Header Files and that Header File is included in the program, but if we wish we can also make the Declarations in the Class in the Main Program File. Can be placed, as done in the program just created.
#include <iostream.h>
#include <conio.h>
class book_bank
{
public:
char title[20];
char author[30];
int page;
float price;
void Display(void);
void Input(void);
};
void main(void)
{
book_bank book1;
clrscr();
book1.Input();
book1.Display();
getch();
}
void book_bank::Display(void)
{
cout<< "Book Name t :"<< title;
cout<< "nBook Author t :"<< author;
cout<< "nBook Page t :"<< page;
cout<< "nBook Price t :"<< price;
}
void book_bank::Input(void)
{
cout<< "Enter Book's Title t";
cin>>title;
cout<< "nEnter Book's Author t";
cin>>author;
cout<< "nEnter Book's Page t";
cin>>page;
cout<< "nEnter Book's Price t";
cin>>price;
}
In this program, Member Functions have been defined outside the class. When a Member Function is defined outside the class, then we need to tell the Compiler that the function that is being defined is Declare in which class the function is being defined.
To tell this, we have to connect that class with Member Function by Scope Resolution Operator. For example, void book_bank :: Input (void) tells the statement compiler that the function being defined is a member function of the function book_bank class.
Whenever we define a function in "C ++", the compiler also has to tell which type of argument the function will take and what kind of value will return. Since this function is not returning any value of any kind, hence the return value of the function is void Declare and this function is not taking any argument of any kind, hence void is also used in the brackets of the function.
All the members of the class book_bank have been made public in the program that we have just mentioned. Since data in OOP is mostly hidden, Variables of Data Members are written in private area.
Like suppose we have to initialize the price and number of pages in the object in a book and in the previous program if book1.price = 123; Else book1.page = 123; If you use Statement, then the program will work and Price and Page Initialize in Object.
0 Comments