Friday, April 19, 2019

How to Declare a Class in C++

What is Class

An instance of a class is called an object and programs can contain any number of classes. As with other types, object types are case-sensitive. Classes provide encapsulation as defined in the Object Oriented Programming (OOP) paradigm. A class can have both data members and functions members associated with it.


The Basic Syntex of Class in C++

class ClsName
{
private : variables declaration;
function declaration;

public : variables declaration;
function declaration;

protected: variables declaration;
 function declaration;
};

And How we Make an Object in C++

ClsName object;

In Object-oriented programming, an object is an instance of a Class. Objects are an abstraction. They hold both data, and ways to manipulate the data. ... Most applications (and other objects) only change this object through this interface.


A Basic Program of Class in C++

ClsName object;




# include <iostream>
# include <iomanip>
using namespace std;

/*
class ClsName
{
private : variables declaration
function declaration

public : variables declaration;
function declaration;

protected : variables declaration;
function declaration;
};
*/
class Emp{
private:
int id;
char name[30];
char dept[30];

public : 
void GetData(void)
{
cout<<"Enter Employee Id: \n";
cin>>id;

cout<<"Enter Name: \n";
cin>>name;

cout<<"Enter department: \n";
cin>>dept;

cout<<"\n\n";
}

void DisInforn(void)
{
cout<<"\n\n----------------------------------------\n\n";
cout<<"Employee Id: "<<id<<endl;
cout<<"Employee Name: "<<name<<endl;
cout<<"Employee Department: "<<dept<<endl;
}
};
int main() {
Emp obj1;
Emp obj2;
Emp obj3;
Emp obj4;
Emp obj5;
/*---------------------------*/

cout<<"Enter Employee Basic Details \n";
obj1.GetData();
obj2.GetData();
obj3.GetData();
obj4.GetData();
obj5.GetData();

cout<<"Employees Basic Details \n";

obj1.DisInforn();
obj2.DisInforn();
obj3.DisInforn();
obj4.DisInforn();
obj5.DisInforn();

return 0;

No comments:

Post a Comment