Sunday, 31 March 2019

Classes and Objects in C++

A class is an extension idea of structure used in C. It is a new way of creating and implementing a user defined data type. The standard C does not all the structure data type to be treated like Built-in Data type.

 For example
struct complex
{
     float x;
     float y;
};

struct complex c1,c2,c3;

The complex numbers c1,c2, and c3 can easily be assigned values using the dot operator, but we cannot add the two complex numbers or subtract one from the other. For example

c3=c1+c2; // is illegal in C.


Another important limitation of C structure is that they do not permit data hiding.

Specifying a class in C++

A class a way to bind the data and its associated functions together. It allows the data to be hidden., if necessary, from external use. When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type. Generally, a class specification has two parts.
  1. class declaration.
  2. class function definitions. 

1. class declaration

The class declaration describes the type and scope of its members. The class function definitions describes how the class functions are implemented.

The general form of a class declaration is


Declaring an Object

When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.

Syntax:

className ObjectName;

Accessing data members and member functions:

The data members and member functions of class can be accessed using the dot(‘.’) operator with the object. For example if the name of object is obj and you want to access the member function with the name printName() then you will have to write obj.printName().

The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object. Accessing a data member depends solely on the access control of that data member.This access control is given by Access modifiers in C++. There are three access modifiers : public, private and protected

Example:

// C++ program to demonstrate
// accessing of data members


    using namespace std;

    class MyClass

    {

        // Access specifier

        public:


        // Data Members

        string name;


        // Member Functions()

        void display()

        {

        cout<<"name is: "<<name;

        }

    };


    int main() {


        // Declare an object of class MyClass

       MyClass obj1;


        // accessing data member

        obj1.name = "RAMU";


        // accessing member function

        obj1.display();

        return 0;

    } 

2. class function definitions.

There are 2 ways to define a member function:
  • Inside class definition
  • Outside class definition

0 comments:

Post a Comment

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

Computer Basics

More

MS Office

More

Database Management

More
Top