ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

About the Object-Oriented Programming Language C++

Updated on September 13, 2017
radhikasree profile image

Radhika has the degree of master in computer applications. She has worked as a faculty with Aptech Computer Education Center for sometime.

A common class declaration format in C++
A common class declaration format in C++ | Source

Conventional or traditional programming method usually deals with basic data types such as integer or character and is quite familiar with the common user. But if the volume of data involved in a problem is more, it becomes difficult to program using conventional programming. In such case, data is organized to form a class (like structure in C) of user-defined type and programming is performed on that data type. We use the term ‘object’ instead of ‘variables’ for the data belonging to the class and so the name object-oriented programming.

Example:

class emp

{

char name[20];

int empcode;

int salary;

};

The class ‘emp’ has 3 basic data type members and the statement

emp A; creates an object A of the class type emp.

C++ Programming Language

Several enhancements made to C programming language gave rise to C++. An intermediate level language combining both the features of a low level and high level language is C++. It was developed by the computer scientist Bjarne Stroustrup at Bell Labs. Being a popular programming language, its applications cover a variety of domains such as system software, embedded systems, device drivers, entertainment applications like video games etc. The added features made it easier to do programming involving objects of a class type.

1. Data encapsulation


Data described within a class (data members) is private and can be accessed only by the functions declared inside the class called member functions. Functions declared outside the class have no access to the data members of the class. This property is termed as data hiding which is the core part of object-oriented programming.

Example:


class student

{

char name[];

int rollno;

public:

void getdata();

void putdata();

};

student A;

void getdetails();

Object A belongs to class ‘student’ and it can access the data members only through the member functions declared in the public section. A.getdata() invoke the getdata() function and reads the data namely name and rollno of the object A but, the function getdetails() has no access to the data members as it is declared outside the scope of the class ‘student’. This binding of data and functions into a single class-type variable is referred to as data encapsulation.

2. Operator overloading


This concept lets operations to do with class data types as we do with basic data types. Operators are overloaded using a function by passing parameters to perform the operations. Member functions or friend functions can overload an operator to do basic operations with the objects of the class to which they belong to.

Example:


Class complex

{

Int real;

Int imag;

public:

void getdata();

void putdata();

complex operator +(int);

}A,B;

This class ‘complex’ has two data members real and imag. The first two functions read and write the object details whereas the third one is the overloaded binary operator member function that adds two objects of the class ‘complex’. This function can be defined as:

complex operator+(int)

{

complex C;

C.real=real+B.real;

C.imag=imag+B.imag;

return(C);

}

The statement C=A+B;

invokes this function and the object A is passed implicitly, but B explicitly. The sum of the values gets stored in C, the result. This is an instance of complex numbers addition.

3. Inheritance


This enhanced feature of C++ allows program development to advanced modes. Like a child born to its parent, new class also can be derived from an existing class. This new class may inherit the characteristics of the existing class which introduces the concept of code reusability. The existing class is called base class and new class becomes the derived class. The base class members are accessible to the derived class depending upon the visibility mode it inherits. The derived class can add data members or member functions to it and can override the base class.

The derived class inherits only public and protected members and not private members. Also usually, new classes are inherited in public or protected mode, rather than in private mode.


How base class members are inherited in public mode

Inheritance in public mode
Inheritance in public mode | Source

Inheritance of base class members in protected mode

Inheritance in protected mode
Inheritance in protected mode | Source

The general form of a derived class is:

class [derived class name]:[visibility mode] [base class name]

Consider a program:


class time

{

Int hr,min,sec;

public:

void gettime();

};

class period:public time

{

Int dur;

public:

int getdur();

}A;

In this program, ‘time’ is the base class and ‘period’ derived class. The statements A.gettime() and A.getdur() would invoke the member functions of both the classes as it is publicly inherited.

Various types of inheritance exist depending upon the problem type. They are multiple inheritance, hierarchical inheritance and hybrid inheritance.

4. Polymorphism

Polymorphism using virtual functions
Polymorphism using virtual functions | Source

This is the ability of a function to exhibit unique behavior when defined in each of the derived classes and invoked through a pointer to an object of the base class. The concept of virtual functions makes this property visible in C++.

Consider this example:


class Org

{

int basic;

public:

void setvalue(int x)

{

basic=x;

}

virtual int salary()

{

return(0);

}

};

class Manager: public Org

{

public:

int salary()

{

return(basic+basic*0.9);

}

};

class Accountant: public Org

{

public:

int salary()

{

return(basic+basic*0.3);

}

};

Let Organization be the base class and Manager, Accountant as the derived classes. The amount of salary given out to the manager and accountant varies and this calculation can be done by using polymorphism property.


Now, if A, B, and C are the objects of Org, Manager and Accountant classes respectively, the statements

Org *x=&A;

Org *y=&B;

Org *z=&C;

would declare pointers of base class(Org) objects to point to these objects A, B and C in the main function. The statements x->setvalue(10000), y->setvalue(6000) and z->setvalue(2000) would set the ‘basic’ value of these pointer objects. Then the statements

x->salary(), y->salary() and z->salary() would print out the salaries of Org, Manager and Accountant classes separately. This property of executing the functions defined in different derived classes with the same name is called polymorphism in C++. This is accomplished by declaring it as a virtual function in the base class. If it were not declared as virtual, the base class function only would have got executed all the time when invoked using a pointer to an object of the base class.

In a class hierarchy, polymorphism plays an important role in solving various problems that have to be dealt within derived classes.

Please vote!

Is C++ easy for you?

See results

© 2013 Radhika Sreekanth

Comments

    0 of 8192 characters used
    Post Comment

    • radhikasree profile image
      Author

      Radhika Sreekanth 4 years ago from Mumbai,India

      Hi Govind Rao,

      I've clearly explained the same in this hub. For more details, read beginning lessons of learners text books of C++ Or OOPs.

    • Govind Rao profile image

      Govind Rao 4 years ago from Bangalore, India

      Hi Radika , i am not getting what is the OOP Definition . can u explain me please.

    • radhikasree profile image
      Author

      Radhika Sreekanth 5 years ago from Mumbai,India

      Thanks sirama for the nice comment.

    • sirama profile image

      sirama 5 years ago

      Nice explanation Radhika. The inheritance mode is real nice.. Why do left private inheritance..

    • radhikasree profile image
      Author

      Radhika Sreekanth 5 years ago from Mumbai,India

      Hi Ruby,

      You read and commented on this.....that's something invaluable for me..thank you for being with me. Yes, I worked as a teacher in the past.

    • always exploring profile image

      Ruby Jean Richert 5 years ago from Southern Illinois

      I am so sorry but this is over my head. I don't understand. Anything mathematical has always been difficult for me. I do hope others will read who can use this. Thank you for sharing..Are you a teacher? Cheers..

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)