Public vs. Private Members

Easy Encapsulation, Access Specifiers & The this Pointer
Solve in Playground →

Problem Statement

Define a `Person` class. Use the `public` access specifier for a string `name` and the `private` access specifier for an integer `age`. Add a public method `setAge(int a)` to safely update the private age, and a public method `display()` to print the details. In `main()`, instantiate the class, read a name and age, and call the methods.

Input Format

A string (name) and an integer (age) separated by space.

Output Format

Print the formatted details.

Constraints

None

Sample Input

Alice 25

Sample Output

Name: Alice, Age: 25

Explanation

Encapsulation hides sensitive data (`private`) while exposing a controlled interface (`public`) to interact with that data.

Starter Code

#include <iostream>
#include <string>

class Person {
    // Define public and private members here
};

int main() {
    // Instantiate Person, read inputs, set data, and display
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used