In: Computer Science
Q#1: Take a real-world scenario and Implement it in
C++ program. Your program
should consist of the following:
1. Constructors
2. Pointers
3. Private and Public keywords
4. Reference by return
5. Const keyword
6. Passing Object as argument
Points to consider before starting:
Please refer to the comments of the program for implementation of all and for more clarity.
#include<bits/stdc++.h>
using namespace std;
// Global variable
int numReferenceByReturn;
// Function declaration
int& funcReferenceByReturn();
class shirt {
public: // Use of public key word
string shirt_color, shirt_size;
// Use of constructor
// Default Constructor
shirt()
{
shirt_color = "White";
shirt_size = "Medium";
}
private: // Use of private keyword
tailor(string shirt_size) const // Use of const keyword
{
// Calculation of cost
int cost = 0;
if(shirt_size == "Medium")
cost = 100;
else if(shirt_size == "Large")
cost = 150;
else
cost = 80;
}
public:
string best_colour = ""; // Initializing it as empty string
void add(shirt s)
{
best_colour = best_colour + s.best_colour; // Adding the colors
}
};
// Reference by return
int& funcReferenceByReturn()
{
return numReferenceByReturn;
}
int main()
{
// Using constructor
shirt s;
cout << "Shirt color: " << s.shirt_color << endl;
cout << "Shirt size: " << s.shirt_size << endl;
// Use of pointer
int v = 20; // actual variable declaration.
int *i; // pointer variable
i = &v; // store address of var in pointer variable
cout << "\nValue of v variable: ";
cout << v << endl;
// Reference by return
funcReferenceByReturn() = 5;
cout << "\nValue of numReferenceByReturn: " << numReferenceByReturn << endl;
s.best_colour = "Black";
// Passing Object as argument
shirt s1;
s1.add(s);
cout << "\nBest color: " << s1.best_colour << endl; // Printing the value we get after passing Object as argument
return 0;
}
Output after running the above program:
Please let me know in the comments in case of any confusion. Also, please upvote if you like.