Program to create a simple class in C++
#include<iostream.h>
class simple
{
private:
void you()
{
cout<<"Hi, i am private, you cannot access in main.";
}
public:
void hello()
{
cout<<"Welcome to hello function.\n";
}
void world()
{
cout<<"You are now in world function.";
}
};
void main()
{
simple obj; //creating object of class simple
cout<<"Welcome to main function.\n";
obj.hello(); //accessing the function hello using the object obj of class simple
obj.world();
//object.you(); //this will give error because the function is private
getch();
}
==OUTPUT==
Welcome to main function.
Welcome to hello function.
You are now in world function.
#include<iostream.h>
class simple
{
private:
void you()
{
cout<<"Hi, i am private, you cannot access in main.";
}
public:
void hello()
{
cout<<"Welcome to hello function.\n";
}
void world()
{
cout<<"You are now in world function.";
}
};
void main()
{
simple obj; //creating object of class simple
cout<<"Welcome to main function.\n";
obj.hello(); //accessing the function hello using the object obj of class simple
obj.world();
//object.you(); //this will give error because the function is private
getch();
}
==OUTPUT==
Welcome to main function.
Welcome to hello function.
You are now in world function.