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 Basic Concepts

Every program in java is a collection of objects communicating via invoking each others methods. Here are few java basic concepts.

Java Class

A class is a template or blueprint which defines the properties, states and behavior that objects of the class support.

Java Object

Object is an instance of a class which has behaviors and states. The properties and behaviors defined in a class are achieved through their objects.

Java Methods

Methods are behaviors which are defined inside a class. Logic is written is methods and many methods can be written in a class.

Instance Variables

Instance variables are used to store the states of objects. They are declared in class outside any method declaration or constructor.

Java Keywords

The keywords are reserved words with predefined meaning in java. They cannot be used by programmer as variable name, class name, method name or any other identifier name. Java has total 50 keywords.

Java Comments

comments are used to help the person reading code to better understand the functionality of the program. They are the integral part of any program.

Java Objects & Classes

Java is an object-oriented programming language and classes are fundamental part of any java program. Individual objects are created from these classes. Below is a java class example.

public class Employee{

int empAge;
String empName;

public void setEmpAge(int age){


public int getEmpAge(){



Java Objects

The java objects are similar to real world objects like human, cat, car etc. Each object has their own states and behaviors. The state of an object in java is stored in fields and behaviors are implemented through methods. The communication between objects are done through methods.

Local Variables

Variables which are defined inside method or constructor body are called local variables. The variable can be accessed inside that method and are destroyed when method completed.

Instance variables

Variables which are defined in class body but outside any method or constructor are instance variables. They are initialized when class is instantiated and can be accessed in method or constructor of that class.

Class Variables

Variables which are defined inside class body but outside any method or constructor with static keyword are called class variables.

Java Constructors

In java each class has a constructor method. If you do not write a constructor method in your class java compiler will automatically bind the default constructor with your class. The constructor method should have same name as of class name. When object of a class is created constructor will be invoked. There can be multiple constructors in same class.

Constructor Example

public class Employee{

public Employee(String name){

// constructor code goes here.



Creating Objects

Objects are created from classes. ‘new’ keyword is used to create object of a class. See the example below.

public class Employee{

public Employee(String empName){

System.out.println(“Employee Name is :” + empName );


public static void main(String []args){

// Creating an object newEmployee
Employee newEmployee = new Employee(“Jhon”);