C++ Classes

The main difference between c and c++ programs is that c++ adds object orientation to the c programs. The classes are the central concept of object-oriented programming. Classes are an expanded concept of data structures which can contain data members and member functions and also called user-defined types.

Defining C++ Classes

A class defines a blueprint for a data type which means it does not actually define any data but defines that what the object of the class will consist of and the operations which can be performed on the object.
class keyword is used to define a class in c++.

class className {


} objects;

The body of the class can contain members which can be data members, functions or access specifiers.

C++ Access Specifiers

The access specifiers modify the access rights for the members of that class. C++ has following access specifiers.

The private Access Specifier

private members of a class can only be accessed from within members of the same class.

The protected Access Specifier

protected members of a class can only be accessed from within members of the same class and also from members of their derived classes.

The public Access Specifier

public members of a class can be accessed from anywhere where the object is visible.

Defining Class Objects

The declaration of the objects are exactly the same as of the declaration of variables of basic types.

className obj1;
className obj2;

C++ Class Example

#include <iostream>

using namespace std;

class Vehicle {
int model;
double mileage;
string make;

int main( ) {
Vehicle car1;
Vehicle car2;

car1.model = 2017;
car1.mileage= 45671;
car1.make = “Honda”;

car2.model = 2015;
car2.mileage= 91175;
car2.make = “Audi”;

cout << “Car 1 is ” << car1.make << ” ” << car1.model << ” with ” << car1.mileage<< ” mileage” <<endl;

cout << “Car 2 is ” << car2.make << ” ” << car2.model << ” with ” << car2.mileage<< ” mileage” <<endl;

return 0;

The Output will be

Car 1 is Honda 2017 with 45671 mileage
Car 2 is Audi 2015 with 91175 mileage

Java Access Modifiers

Access modifiers are used to set access levels for variables, methods, constructors and classes. A special keyword is used for each access modifier except the default. Default access modifier has no keyword. There are following type of access modifiers in java.

  • default
  • public
  • private
  • protected

In java there are some non-access modifiers which are static, synchronized, abstract, native, volatile, transient etc.

Default Access Modifier

The default access modifier is not explicitly declared for a class, variable or method. Any method or variable without any access modifier is accessible in any class in same package.

String chapter= “5”;

int countPages() {
/*some code here*/

Public Access Modifier

The public access modifier is declared with public keyword and it is accessible everywhere.

public class HelloWorld{

public void msg(){
System.out.println(“Hello World”);


Private Access Modifier

Any variable, method or constructor declared with private access modifier can be accessed only in that class in which it is defined. it is the most restrictive access modifier. We cannot declare class and interfaces with private access modifier in java.

public class Car {

private int carMake;

public String getCarMake() {
return this.carMake;
public void setCarMake(String carMake) {
this.carMake = carMake;


Protected Access Modifier

Any variable, method or constructor declared with protected access modifier can only be access in sub-classes within package or any other package through inheritance. Class and interface cannot be declared protected also methods and variables in an interface cannot be declared protected.

public class HelloWorld{

protected void msg(){
System.out.println(“Hello World”);