VC++ tutorial

Visual C++ Examples

How To Add Menu In VC++

adplus-dvertising
Classes In VC++
Previous Home Next

A class is an organization of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object.

class consists of data members and methods. A member function determine the behavior of the class.

Syntax:

Class class_name
{
    Data Members;
    Methods;
}

Example:

#include <iostream> 
using namespace std;
class Box
{
public:
double length; 
double breadth; 
double height; 
}
int main( )
{
Box Box1; 
 double volume = 0.0; 
Box1.height = 10; 
Box1.length = 5; 
Box1.breadth = 7;
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl; 
return 0;

Access Specifiers:

public: If a class member is public, it can be used anywhere without the access restrictions.



#include <iostream>

using namespace std;


class Employee
{

public:


  char name;
  int salary;
};


int main()
{
    
    Employee obj;

 
    cout<<"Enter the Name :";
    cin>>obj.name;

    cout<<"Enter the salary :";
    cin>>obj.salary;

  
    cout << obj.name << ": " << obj.salary << endl;

  
    return 0;
}

private: If a class member is private, it can be used only by the members and friends of class.

protcted: If a class member is protected, it can be used only by the members and friends of class and the members and friends of classes derived from class.

Previous Home Next