C++ Program To Show Use Of Objects In Classes - Coding Bot

Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Sunday, 19 May 2013

C++ Program To Show Use Of Objects In Classes

Hi Guys,
In this post, we'll learn what are objects, the most important entity for which C++ is known better than C language. The main reason of C++ programming is to add the ability to create objects and classes are a central feature of C++ that support OOPS aka Object-Oriented Programming and classes are often called as user-defined types.


Objects are the basic run-time entities in an object-oriented system. They may represent a person, place, a bank account, a table of data or any other item that the program has to handle.When a program is executed, the objects interact by sending messages to one another.

Objects can interact without having to know the details of each other's data or code. Each object contains data, and code to manipulate the data. Well here is an example:

Objects In C++
Two Ways Of Representing An Object


C++ Code To Show Use Of Objects In Classes:



/* Double-Click To Select Code */

#include<iostream.h>
#include<conio.h>

class product  //Class name
{
 private:  // Private data member not accessible by object
  int number;
  float cost;
 
 public:
  void getdata(int a,float b);  //Function declaration
  void putdata();
};

void product::getdata(int a,float b)  //Function definition
{
 number = a;
 cost  = b;
}

void product::putdata()
{
 cout<<"Number : "<<number<<endl;
 cout<<"Cost : "<<cost<<endl;
}

void main()
{
 clrscr();
 product p;   //Declaration of object
 cout<<"\nObject P"<<"\n";
 
 p.getdata(100,299.95);  //Only public members are accessible by object
 p.putdata();
 
 product q;
 cout<<"\nObject Q"<<"\n";
 
 q.getdata(200,175.50);
 q.putdata();
 getch();
}

Program Output:


Objects In C++

Please Comment If You Liked The Post .


Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: