Access Class members in C++
In C++, a class is a fundamental concept for object-oriented programming (OOP). A class defines a blueprint for creating objects (instances) with shared attributes (data members) and behaviours (member functions). You must first create an instance of the class and then access its members with the dot operator (.) to access the members of a class in C++.
1. Defining a Class:
You can use the class keyword followed by the class name to create a class. You can define data members and member functions (methods) in the class.
class MyClass {
public:
// Data members
int data_member1;
double data_member2;
// Member functions
void memberFunction1() {
// Function code here
}
};
2. Creating Objects:
You need to create objects (instances) of the class to use the class and access its members. Objects are instances of the class with their copies of data members.
MyClass myObject;
// Creating an object of MyClass
3. Accessing Data Members:
You can access the data members of an object using the dot operator (.). See the example below.
myObject.data_member1 = 42;
// Assigning a value to data_member1
double value = myObject.data_member2;
// Accessing data_member2
4. Calling Member Functions:
You can also call member functions of an object using the dot operator. For example:
myObject.memberFunction1();
// Calling memberFunction1
5. Access Specifiers:
In C++, you define members as public, private, or protected. It determines their accessibility from outside the class.
- Public: Members can be accessed from anywhere.
- Private: Members are only accessible within the class.
- Protected: Similar to private, but accessible by derived classes.
class MyClass {
public:
int public member; // Public Member
Private:
int private member; // Private Member
Protected:
int protected member; // Protected member
};
6. Access Control:
Access control mechanisms allow you to restrict access to certain class members. It's good practice to private data members and provide public member functions to manipulate them. It is known as encapsulation.
class MyClass {
private:
Int private data;
Public:
void setPrivateData(int value) {
private data = value;
}
int getPrivateData() {
return privateData;
}
};
7. Constructor and Destructor:
Classes can have constructors and destructors, special member functions for object initialization and cleanup.
class MyClass {
public:
MyClass() {
// Constructor code here
}
~MyClass() {
// Destructor code here
}
};
Algorithm:
Step 1: Define a class MyClass with private and public members.
Step 2: In the class definition, declare a private integer data member privateData and a public double data member publicData.
Step 3: Define a constructor for MyClass that initializes private data to 0 when an object is created.
Step 4: Implement member functions setPrivateData and getPrivateData to encapsulate access to the private data member private data.
Step 5: Create an object myObject of class MyClass in the main function.
Step 6: Access and modify the public data member publicData using the dot operator.
Step 7: Use the member functions setPrivateData and getPrivateData to manipulate and retrieve the private data member private data.
Step 8: Displays information about public and private data members using the displayInfo member function.
Step 9: Finally, output the accessed values of public and private data members.
Implementation:
Let’s see the implementation based on the algorithm defined above.
Program:
#include <iostream>
// Class definition
class MyClass {
private:
int privateData;
public:
// Constructor
MyClass() {
privateData = 0;
}
// Member function to set private data
void setPrivateData(int value) {
privateData = value;
}
// Member function to get private data
int getPrivateData() {
return privateData;
}
// Public data member
double publicData;
// Member function to display information
void displayInfo() {
std::cout << "Private Data: " << privateData << std::endl;
std::cout << "Public Data: " << publicData << std::endl;
}
};
int main() {
// Creating an object of MyClass
MyClass myObject;
// Accessing public data member
myObject.publicData = 42.5;
// Using member functions to set and get private data
myObject.setPrivateData(100);
int privateValue = myObject.getPrivateData();
// Displaying information
myObject.displayInfo();
// Accessing public data member
std::cout << "Accessed Public Data: " << myObject.publicData << std::endl;
std::cout << "Accessed Private Data: " << privateValue << std::endl;
return 0;
}
Output:
Private Data: 100
Public Data: 42.5
Accessed Public Data: 42.5
Accessed Private Data: 100
Explanation:
1. Class Definition:2
In C++, classes serve as blueprints for creating objects. The code starts by defining a class called MyClass. Inside the class, two types of members are declared:
- Private Data Member: private data is declared as an integer and marked as private. It means that it can only be accessed within the class, not outside.
- Public Data Member: public data is declared as a double and marked as public. It makes it accessible both within and outside the class.
2. Constructor:
A constructor is a special member function called when an object of the class is created. In this case, the constructor for MyClass is defined. Inside the constructor, the private data member private data is initialized to 0.
3. Member Functions:
Two member functions are defined within the class:
- setPrivateData(int value): This function allows you to set the value of the private data member private data.
- getPrivateData(): This function allows you to retrieve the value of the private data member private data.
- displayInfo(): This function displays information about private and public data members. It uses the std::cout statement to print these values to the console.
4. Main Function:
The main function is the entry point of the program. Here's what it does:
- It creates an object myObject of the MyClass class.
- It accesses and modifies the public data member public data of myObject by using the dot operator (myObject.publicData).
- It sets the private data of myObject to 100 using the setPrivateData member function and retrieves its value using getPrivateData.
- It calls the displayInfo member function to display information about both data members.
- Finally, it prints the accessed values of public and private data members to the console.
Complexity analysis:
Time Complexity:
Creating an object: O(1) - Constant memory allocation and initialization time.
Calling member functions: O(1) - Simple assignments and return operations.
Printing to the console: O(1) - Usually efficient, considered constant time.
Accessing member variable: O(1) - Direct retrieval with constant time complexity.
Space complexity:
The space complexity of creating an object of the MyClass class (MyClass myObject;) is O(1) because the object's size is fixed and doesn't depend on the number of objects created.
Local variables, like privateValue, also have a space complexity of O(1) because they use a constant amount of memory regardless of the input size.
Approach 2:
Inheritance:
When you have a base class with protected members, derived classes can access those members as if they were their own. It is used in inheritance to allow controlled access to certain members.
Program:
#include <iostream>
// Base class
class Base {
protected:
int protectedData; // Protected member
public:
Base() : protectedData(0) {}
void setProtectedData(int value) {
protectedData = value;
}
};
// Derived class
class Derived : public Base {
public:
void printProtectedData() {
// Accessing the protected member from the derived class
std::cout << "Protected Data from Derived Class: " << protectedData << std::endl;
}
};
int main() {
// Create an object of the derived class
Derived derivedObject;
// Accessing the protected member of the base class through a member function of the derived class
derivedObject.setProtectedData(42);
derivedObject.printProtectedData();
return 0;
}
Output:
Protected Data from Derived Class: 42
Explanation:
1. In this example, we define a base class Base with a single protected data member protectedData. This member is marked as protected, meaning it can be accessed by derived classes but not directly from outside the class.
2. The constructor for the base class initializes protected data to 0.
3. The base class also includes a public member function, setProtectedData, that allows setting the value of protected data.
4. We define a derived class Derived that inherits publicly from the base class Base. It means that the protected members of the base class are accessible in the derived class.
5. Inside the derived class, we have a member function, printProtectedData, that can access and print the value of protected data.
6. In the main function, we create an object derived object of the derived class.
7. We use the setProtectedData member function inherited from the base class to set the value of protectedData to 42 within the derived class.
8. Finally, we call the printProtectedData member function of the derived class, which accesses and prints the value of protectedData.
Time Complexity:
The time complexity of this code is primarily determined by the operations performed within the code, such as function calls and assignments. These operations are typically O(1), meaning they take constant time, as they do not depend on the input size.
Space Complexity:
The space complexity of this code is also O(1), as it does not allocate memory dynamically based on the input size. The memory usage is constant and does not grow with the input.