How to declare objects in OOP C++?

By: Prof. Dr. Fazal Rehman Shamil | Last updated: March 3, 2022

How to declare objects in OOP C++?

In this tutorial, we will learn about the following;

How to declare an object in OOP C++?

Syntax to declare objects

Serial #Code
1int main()
2{
3furniture f;
4f.show_price();
5}

Line 1 and 2: We are starting the main function

Line 3: furniture is the name of class and f is the object of class furniture

Line 4: f is class name and . is used to access the function from a class and show_price() is the function name.

Line 5: Ending of the main function.

Program:

Serial#Code
1#include<iostream>
2using namespace std;
3class furniture
4
5private:
6 int price=8;
7 public:
8 int show_price() 
9
10cout<<“show price=”<<endl<<price; 
11}
12};
13int main()
14{
15 furniture f; 
16f.show_price();
17}

 

Leave a Reply