LearnFrenzy
Sign in Take a free test

Static and Friend Members · Exercise 4 of 4

Static and Friend Members Static and Friend Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs that use static members and friend functions for real designs. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Medium

Write a ConfigManager singleton that stores key/value settings, prevents copying, and is created only on first use. Show that two references refer to the same object.

Question page & discussion
Question 2 Easy

Write a SavingsAccount class with a private static interest rate (with a static setter that validates it) and a static count of accounts; apply monthly interest to every account before and after a rate change.

Question page & discussion
Question 3 Medium

Write a Session class with static counters for total sessions ever created and sessions currently alive, correct for copies and moves too, and show the counts at several points.

Question page & discussion
Question 4 Easy

Write a Point class with friend operators >> (reading the format (x,y)) and << (printing it), read points until the input ends and print each with its distance from the origin.

Question page & discussion
Question 5 Medium

Write a Money class (stored in paise) with an implicit constructor from rupees and hidden-friend operators +, −, ==, < and <<, so that mixed expressions like 100 + m work.

Question page & discussion
Question 6 Medium

Write a Sensor class that keeps its calibration offset and raw readings private, and a friend class SensorTester that can inspect and recalibrate it, while normal users see only calibrated values.

Question page & discussion
Question 7 Medium

Write classes Rectangle and Circle with private dimensions, and one function declared as a friend of both that reports which shape has the larger area for several pairs.

Question page & discussion
Question 8 Hard

Write a template base class InstanceCounter<T> that counts created and alive objects for each derived class separately (correct for copies), and use it in two unrelated classes.

Question page & discussion
Question 9 Hard

Write a Color class with a private constructor and a static get(name) that returns a shared object, creating each color only once and caching it in a static map; count how many objects were really created.

Question page & discussion
Question 10 Medium

Write Matrix2 and Vec2 classes with private data and a single friend operator*(const Matrix2&, const Vec2&) that both classes trust; rotate a vector by 90° twice.

Question page & discussion
All exercises doneBack to Static and Friend MembersRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links