Question

In: Computer Science

Q#1: Take a real-world scenario and Implement it in C++ program. Your program should consist of...

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

Solutions

Expert Solution

Points to consider before starting:

  • Here, I have taken the real-world example of the shirt industry.
  • I have considered "Black" as the best colour for the shirt.
  • The default size of a shirt is "Medium".
  • The default colour of the shirt is "White".

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.


Related Solutions

Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Develop a real world scenario that illustrates the Order of Operations . Your post should include...
Develop a real world scenario that illustrates the Order of Operations . Your post should include a mathematical expression that represents your unique scenario, and a step by step explanation of how the Order of Operations is used to find your answer.
Q-1) Write a program that mimics a calculator. The program should take as input two integers...
Q-1) Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. For division, if the denominator is zero, output an appropriate message. Some sample outputs follow: 3 + 4 = 7 13 * 5 = 65
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to...
C Programming Question: Q) Write a C - program to implement an Uprooted version (Save to parent pointer instead of child pointer, ie. parent of top is null) of Kruskal's Minimum Spanning Tree with adjacency list and min-heap as the additional data structure. Note: Please implement it in C and also keep the above conditions in mind. You can take your time. Thank you.
This programming assignment will consist of a C++ program. Your program must compile correctly and produce...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting described in the Required Program Development Best Practices document that has been discussed in class and is posted to the eLearning system. Please see this descriptive file on the eLearning system for more details. The name to use in the main configuration screen text box Name: [ ] in...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Describe any real world engineering application your project represents. Describe similarities and differences between your project and the real world scenario.
Arduino ProjectDescribe any real world engineering application your project represents. Describe similarities and differences between your project and the real world scenario.Write algorithm and flow chart for the program.Required equipment.Objective is too great program design with algorithm and flowchart for annany day engineering application.
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree with a string at each node.  This tree stores the knowledge that your program currently knows.  At leaf nodes, the strings should contain the names of animals, while interior nodes should contain yes/no questions.  The program should start at the root and ask the question stored at the root.  If the user answers the question no, the program traverses the left branch of the tree, while if the...
Develop a conceptual data model for the following scenario: The data model should consist of the...
Develop a conceptual data model for the following scenario: The data model should consist of the usual 5 components: E-R diagram, Entity Types (including entity type identifiers), assumptions, additional constraints, and limitations: It is desired to develop a shop database for a shop of different classes of products. A shop sells different classes of products to customers. For each class, it is required to keep the following information: class identification number (class-id) and class name. Each class has one or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT