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