0% found this document useful (0 votes)
5 views3 pages

C++ Class Fundamentals and Usage

This document provides an overview of C++ classes, including their structure, constructors, access specifiers, and function definitions. It explains how to use getters and setters for data encapsulation and demonstrates these concepts with code examples. Key points include the use of public, private, and protected members, as well as the scope operator for defining functions outside the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C++ Class Fundamentals and Usage

This document provides an overview of C++ classes, including their structure, constructors, access specifiers, and function definitions. It explains how to use getters and setters for data encapsulation and demonstrates these concepts with code examples. Key points include the use of public, private, and protected members, as well as the scope operator for defining functions outside the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Classes Overview

This document demonstrates how classes in C++ use constructors (with and without parameters),
access specifiers (private, public, protected), functions defined inside and outside the class, and
getter/setter methods.

1. Basic Class Structure


class Car {
public:
string brand;
string model;
int year;
};
This is a simple class with three public attributes.

2. Constructors (Default and Parameterized)


class Car {
private:
string brand;
string model;
int year;

public:
// Default constructor
Car() {
brand = "Unknown";
model = "Unknown";
year = 0;
}

// Parameterized constructor
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}

void displayInfo() {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << en
}
};

int main() {
Car car1; // Calls default constructor
Car car2("Toyota", "Camry", 2022); // Calls parameterized constructor
[Link]();
[Link]();

return 0;
}

3. Private, Public, and Protected Members


class Base {
private:
int privateVar;

protected:
int protectedVar;

public:
int publicVar;

void show() {
cout << "Private: " << privateVar << ", Protected: " << protectedVar << ", Pub
}
};

class Derived : public Base {


public:
void accessMembers() {
// privateVar = 5; // ■ Not accessible
protectedVar = 10; // ■ Accessible in derived class
publicVar = 15; // ■ Accessible
}
};

4. Defining Functions Outside the Class Using the Scope Operator


(::)
class Student {
private:
string name;
int age;

public:
void setDetails(string n, int a);
void display();
};

// Defining member functions outside the class


void Student::setDetails(string n, int a) {
name = n;
age = a;
}
void Student::display() {
cout << "Name: " << name << ", Age: " << age << endl;
}

5. Getters and Setters


class Employee {
private:
string name;
double salary;

public:
void setName(string n) { name = n; }
void setSalary(double s) { salary = s; }

string getName() { return name; }


double getSalary() { return salary; }
};

int main() {
Employee emp;
[Link]("Alice");
[Link](5000.0);

cout << [Link]() << " earns $" << [Link]() << endl;

return 0;
}

In summary, classes in C++ allow encapsulation of data and behavior. Constructors help initialize
objects, access specifiers control visibility, and the scope operator (::) allows defining methods outside
the class body. Getters and setters provide controlled access to private data.

You might also like