C++ class Introduction

C++ class Introduction

The most important feature of C++ is the class. it is significance is highlighted by the fact that stroustrup initially gave the name C with classes to this new language. A class is an extension of the structure used in C language. It is new way of creating and implementing a user defined data type we shall discuss, in this tutorial, the concept of the class by first reviewing the traditional structure found in C and then the ways in which classes can be designed implemented and applied

C STRUCTURE REVISITED

We know that one of the unique features of the C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handle a group of logically related data items. It is a user defined data types with a template that serves to define its data properties. once the structure type has been defined, we can create variables of that structure type using declarations that are similar to the built in type declaration.
for example consider the following declaration

structure student
{
char name[20];
int roll;
float total_mark;
};

The keyword struct declares student as a new data type that can hold three fields (data members)of different Data types. These fields are known as elements or structure members . The identifier student

which is referred to as structure tag or structure name , can be used to create variable of type student

Example

struct student A; //C declaration A is variable of type student and has three member variable as defined by the template.

Member variable can be accessed by using the dot or period operator as follows below

strcpy(A.name,"nitish");
A.roll=900;
a.total_mark=3444.3;
Finaltotal=a.total_mark+5;

structure can have arrays, pointers or structures as members

LIMITATIONS OF C STRUCTURES

The standard C does not allow the struct data type to be treated like build in type for example consider The following structure

structure Emp
{
float x;
float y;
};
struct Emp e1,e2,e3;

The complex number e1,e2 and e3 can easily be assigned values using the dot operator, but we cannot add two complex numbers or subtract one from the other for example
e3=e1+e2;
is illegal in C

Another Important limitation of C structure of C structures is that they do not permit data hiding. structure members can be directly accessed by the structure variable by any function anywhere in their scope. in other words, the structure members are public members

EXTENSIONS TO STRUCTURES

C++ support all the features of structures as defined in C. but C++ has expanded its capabilities further to suit its OOPS philosophy. It attempts to bring the user defined data types as close as possible to the built in data types, and also provides a facility to hide the data which is one of the main concept of Object oriented programming. Inheritance, a mechanism by which one type can inherit characteristics from other types, in also supported by C++.

In C++ language, a structure can have both variable and functions as members. it can also declare some of its members as private so they cannot be accessed directly by the external functions

IN C++ language, The structure names are stand alone and can be used like any other type names. In other words, The keyword struct can be omitted in the declaration of structurevariable. for example, we can declare the student variable A as

Student A; //C++ declaration
Remember, this is an error in C

C++ language incorporates all these extensions in another user defined type known as class. There is very little syntactical difference between structure and classes in C++ and , therefore they can be used interchangeably with minor modifications. since class is a specially introduced data type in c++ language, most of the C++ programmers tend to use the structures for holding only data, and classes to hold both the data and functions. Therefore we will not discuss structures any further

Note: The only difference between a structure and a class in c++ is that, by default, the members of a class are private, while, by default, the members of a structure are public

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise