-- --

Classes in C++

A Class is a User Defined Data Type. It may contain functions, variables or other pre-defined  data types. Classes basically encapsulate the data and methods. The syntax to declare a class is

class Test
{
};

A class must be ended with a semicolon.
Objects in C++:- The variables of data type Class are called objects. The objects of a particular class can access the variables and member functions of that class. Objects are declared similarly as normal variables.

datatype variable;
ClassName ObjectName;

A class can have multiple objects. Every object has different copies of the variables but a single copy is shared between the objects in case of functions.
A Simple program to illustrate the uses of classes and objects-

#include<iostream.h>
#include<conoi.h>
class Book
{
private:
char name[15], author[15];
int rack;
public:
void input()
{
cout<<”Enter the name of the book”;
cin>>name;
cout<<”Enter the author’s name”;
cin>>author;
cout<<”Enter the rack number to store the book”;
cin>>rack;
}
void out()
{
cout<<”The book “<<name<<” by “<<author<< “is stored in rack number “<<rack;
}
};
void main()
{
Book one;
one.input();//the private variables are being used outside the class BUT WITH THE HELP OF PUBLIC MEMBER FUNCTION
one.out();
getch();
}

 

kaliadevansh has written 15 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>