UNIT 4:
Points to remember for C++ Enum
enum improves type safety
enum can be easily used in switch
enum can be traversed
enum can have fields, constructors and methods
enum may implement many interfaces but cannot extend any class because it internally
extends Enum class
[5:23 PM, 1/12/2025] Ab: https://www.geeksforgeeks.org/inline-functions-cpp/
[5:29 PM, 1/12/2025] Ab: https://www.geeksforgeeks.org/macros-and-its-types-in-c-cpp/
[6:29 PM, 1/12/2025] Ab: #include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal can eat." << endl;
}
void sleep() {
cout << "This animal can sleep." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "The dog can bark." << endl;
}
};
int main() {
// Creating an object of the derived class
Dog myDog;
// Accessing base class methods using the derived class object
myDog.eat();
myDog.sleep();
// Accessing the derived class method
myDog.bark();
return 0;
}
[6:29 PM, 1/12/2025] Ab: https://www.geeksforgeeks.org/function-overloading-vs-function-
overriding-in-cpp/
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() { // Virtual function
cout << "This is the Base class function." << endl;
}
};
class Derived : public Base {
public:
void display() override { // Overrides the Base class function
cout << "This is the Derived class function." << endl;
}
};
int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj; // Base class pointer pointing to a Derived class object
basePtr->display(); // Calls the Derived class function due to the virtual keyword
return 0;
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { // Virtual function
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override { // Overriding the base class function
cout << "Dog barks" << endl;
};
int main() {
Animal* animal;
Dog dog;
animal = &dog; // Base class pointer pointing to derived class object
animal->sound(); // Calls the overridden function in the derived class
return 0;
[11:07 PM, 1/12/2025] Ab: https://www.geeksforgeeks.org/cpp-static-data-members/
[11:10 PM, 1/12/2025] Ab: The :: in C++ is known as the scope resolution operator. It is used
to specify the scope in which a particular identifier (like a variable, function, or class) is
defined.
https://www.geeksforgeeks.org/type-conversion-in-c/
In your code, operation :: get() means that the function get() is a member of the class
operation. The :: operator is used here to define the body of the get() function outside the
class declaration.
Why use ::?
When defining a member function of a class outside the class definition, you need to tell the
compiler that this function belongs to the class. The :: operator connects the function
definition to the class it belongs to.
UNIT5:
n C++, static member functions and static data members are shared by all objects of a
class. Static data members are associated with the class rather than any specific
object, and static member functions can only access static data members or other
static member functions.
Here's an example demonstrating the use of static member functions and static data members:
Example: Static Member Function and Static Data Member in C++
cpp
Copy code
#include <iostream>
using namespace std;
class Counter {
private:
static int count; // Static data member
public:
// Static member function to access static data
static void increment() {
count++;
}
static void decrement() {
count--;
}
// Static member function to get the current value of count
static int getCount() {
return count;
}
};
// Define and initialize the static data member outside the class
int Counter::count = 0;
int main() {
// Access static member functions without creating an object
cout << "Initial count: " << Counter::getCount() << endl;
Counter::increment(); // Increment the count
Counter::increment();
cout << "Count after incrementing twice: " << Counter::getCount() <<
endl;
Counter::decrement(); // Decrement the count
cout << "Count after decrementing: " << Counter::getCount() << endl;
return 0;
}
Output:
yaml
Copy code
Initial count: 0
Count after incrementing twice: 2
Count after decrementing: 1
Key Points:
1. Static Data Member:
o Declared using the static keyword inside the class.
o Must be defined and initialized outside the class using
ClassName::memberName.
o Shared among all objects of the class, meaning there is only one copy of the
static data member.
2. Static Member Function:
o Declared using the static keyword inside the class.
o Can be called without creating an object of the class.
o Can access only other static members (both data and functions) of the class.
This example demonstrates how static members provide class-level functionality that is independent
of any particular object. Let me know if you'd like further assistance!
https://www.geeksforgeeks.org/cpp-static-data-members/
https://www.javatpoint.com/cpp-constructor
copy constructor from chatgpt
#include <iostream>
using namespace std;
class CopyExample {
private:
int number;
public:
// Parameterized constructor
CopyExample(int num) {
number = num;
}
// Copy constructor
CopyExample(const CopyExample &obj) {
number = obj.number; // Copy value from the existing object
}
void display() {
cout << "Number: " << number << endl;
}
};
int main() {
CopyExample obj1(42); // Call parameterized constructor
CopyExample obj2 = obj1; // Call copy constructor
cout << "Original object: ";
obj1.display();
cout << "Copied object: ";
obj2.display();
return 0;
}
https://www.geeksforgeeks.org/operator-overloading-cpp/
Virtual base classes offer a way to save space and avoid ambiguities in
class hierarchies that use multiple inheritances. When a base class is
specified as a virtual base, it can act as an indirect base more than once
without duplication of its data members. A single copy of its data members
is shared by all the base classes that use virtual
base. https://www.geeksforgeeks.org/virtual-base-class-in-c/
In C++, inheritance allows a class (child or derived class) to acquire the properties and
behaviors of another class (parent or base class). There are several types of inheritance, each
suited for different scenarios.
Types of Inheritance in C++:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Let’s go through each type with an explanation and code example:
1. Single Inheritance
A single derived class inherits from one base class.
Example:
#include <iostream>
using namespace std;
class Base {
public:
void showBase() {
cout << "This is the Base class." << endl;
}
};
class Derived : public Base {
public:
void showDerived() {
cout << "This is the Derived class." << endl;
}
};
int main() {
Derived obj;
obj.showBase(); // Access base class function
obj.showDerived(); // Access derived class function
return 0;
}
Output:
This is the Base class.
This is the Derived class.
2. Multiple Inheritance
A single derived class inherits from multiple base classes.
Example:
#include <iostream>
using namespace std;
class Base1 {
public:
void showBase1() {
cout << "This is Base1 class." << endl;
}
};
class Base2 {
public:
void showBase2() {
cout << "This is Base2 class." << endl;
}
};
class Derived : public Base1, public Base2 {
public:
void showDerived() {
cout << "This is the Derived class." << endl;
}
};
int main() {
Derived obj;
obj.showBase1(); // Access Base1 class function
obj.showBase2(); // Access Base2 class function
obj.showDerived(); // Access derived class function
return 0;
}
Output:
This is Base1 class.
This is Base2 class.
This is the Derived class.
3. Multilevel Inheritance
A class is derived from another derived class, forming a chain of inheritance.
Example:
#include <iostream>
using namespace std;
class Base {
public:
void showBase() {
cout << "This is the Base class." << endl;
}
};
class Intermediate : public Base {
public:
void showIntermediate() {
cout << "This is the Intermediate class." << endl;
}
};
class Derived : public Intermediate {
public:
void showDerived() {
cout << "This is the Derived class." << endl;
}
};
int main() {
Derived obj;
obj.showBase(); // Access Base class function
obj.showIntermediate();// Access Intermediate class function
obj.showDerived(); // Access Derived class function
return 0;
}
Output:
This is the Base class.
This is the Intermediate class.
This is the Derived class.
4. Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
Example:
#include <iostream>
using namespace std;
class Base {
public:
void showBase() {
cout << "This is the Base class." << endl;
}
};
class Derived1 : public Base {
public:
void showDerived1() {
cout << "This is Derived1 class." << endl;
}
};
class Derived2 : public Base {
public:
void showDerived2() {
cout << "This is Derived2 class." << endl;
}
};
int main() {
Derived1 obj1;
Derived2 obj2;
obj1.showBase(); // Access Base class function from Derived1
obj1.showDerived1(); // Access Derived1 function
obj2.showBase(); // Access Base class function from Derived2
obj2.showDerived2(); // Access Derived2 function
return 0;
}
Output:
This is the Base class.
This is Derived1 class.
This is the Base class.
This is Derived2 class.
5. Hybrid Inheritance
A combination of two or more types of inheritance (e.g., hierarchical and multiple
inheritance). To avoid ambiguity, the virtual base class is used when required.
Example:
#include <iostream>
using namespace std;
class Base {
public:
void showBase() {
cout << "This is the Base class." << endl;
}
};
class Intermediate1 : virtual public Base {
public:
void showIntermediate1() {
cout << "This is Intermediate1 class." << endl;
}
};
class Intermediate2 : virtual public Base {
public:
void showIntermediate2() {
cout << "This is Intermediate2 class." << endl;
}
};
class Derived : public Intermediate1, public Intermediate2 {
public:
void showDerived() {
cout << "This is the Derived class." << endl;
}
};
int main() {
Derived obj;
obj.showBase(); // Access Base class function without ambiguity
obj.showIntermediate1(); // Access Intermediate1 class function
obj.showIntermediate2(); // Access Intermediate2 class function
obj.showDerived(); // Access Derived class function
return 0;
}
Output:
This is the Base class.
This is Intermediate1 class.
This is Intermediate2 class.
This is the Derived class.
Summary of Inheritance Types:
Type Description
Single One class inherits from one base class.
Multiple One class inherits from two or more base classes.
Multilevel A chain of inheritance (class inherits from a derived class).
Hierarchical Multiple derived classes inherit from the same base class.
Hybrid Combines two or more types of inheritance (e.g., multiple and hierarchical).
Let me know if you'd like more details or examples for a specific type!
https://www.geeksforgeeks.org/exception-handling-c/?ref=lbp
The -> operator in C++ is used to access a member (like a variable or a function) of a
class or struct through a pointer. It is a shorthand for dereferencing a pointer and then
accessing the member.
Syntax
cpp
Copy code
pointer->member
This is equivalent to:
cpp
Copy code
(*pointer).member
The -> operator dereferences the pointer and accesses the member in a single step, making
the code cleaner and easier to read.
Examples
1. Accessing Members Using a Pointer
cpp
Copy code
#include <iostream>
using namespace std;
class Example {
public:
int value;
void show() {
cout << "Value: " << value << endl;
}
};
int main() {
Example obj;
obj.value = 42;
Example* ptr = &obj; // Pointer to the object
// Access members using the -> operator
ptr->value = 100; // Modify value through pointer
ptr->show(); // Call member function through pointer
return 0;
}
Output:
makefile
Copy code
Value: 100
2. Difference Between -> and .
Use . to access members when you have an object.
Use -> to access members when you have a pointer to an object.
Example:
cpp
Copy code
#include <iostream>
using namespace std;
class Example {
public:
int value;
void show() {
cout << "Value: " << value << endl;
}
};
int main() {
Example obj;
Example* ptr = &obj;
obj.value = 50; // Using .
obj.show(); // Using .
ptr->value = 100; // Using ->
ptr->show(); // Using ->
return 0;
}
Output:
makefile
Copy code
Value: 50
Value: 100
3. Using -> with Dynamic Memory Allocation
When creating objects dynamically, you typically access their members using the ->
operator.
cpp
Copy code
#include <iostream>
using namespace std;
class Example {
public:
int value;
void show() {
cout << "Value: " << value << endl;
}
};
int main() {
Example* ptr = new Example; // Dynamically allocate an object
ptr->value = 25; // Access member using ->
ptr->show(); // Call function using ->
delete ptr; // Free allocated memory
return 0;
}
Output:
makefile
Copy code
Value: 25
When to Use ->
1. Pointer to Object:
o Use -> when working with pointers to objects or structures.
2. Dynamic Allocation:
o When creating objects dynamically with new, use -> to access their members.
In C++, the this pointer is an implicit pointer available inside non-static member functions
of a class. It points to the current object (the object on which the member function is being
called). It allows you to access the members of the current object, and it is often used to
differentiate between member variables and function parameters when they have the same
name.
Key Points about the this pointer:
1. It is a pointer: It points to the object for which the member function is called.
2. It is automatically available: You don’t need to explicitly declare or pass it.
3. It is read-only: You cannot modify the this pointer itself (i.e., it cannot point to a different
object).
4. It is only available in non-static member functions: Static functions do not have access to
this.
Syntax:
this->member; // Access member of the current object using the 'this'
pointer.
Example Code using this pointer
1. Basic Example of Using this
#include <iostream>
using namespace std;
class Example {
private:
int value;
public:
// Constructor to initialize value
Example(int value) {
this->value = value; // Using 'this' pointer to access the member
variable
}
// Member function to display the value
void display() {
cout << "Value: " << this->value << endl; // Using 'this' to access
member variable
}
};
int main() {
Example obj(100);
obj.display();
return 0;
}
Output:
Value: 100
Explanation:
In the constructor, the parameter value has the same name as the member variable
value.
The this->value refers to the member variable, while value without this refers to the
constructor parameter. So, this->value = value; assigns the constructor parameter to
the member variable.
2. Using this in Function with Same Name Variables
Here is another example to differentiate between member variables and local variables using
this.
#include <iostream>
using namespace std;
class Example {
private:
int value;
public:
// Constructor to initialize value
Example(int value) {
this->value = value; // This distinguishes the member variable from
the parameter
}
// Function to set a new value
void setValue(int value) {
this->value = value; // Using 'this' to refer to the member
variable
}
// Member function to display the value
void display() {
cout << "Member value: " << this->value << endl; // Accessing the
current object's member variable
}
};
int main() {
Example obj(10);
obj.display(); // Displays the initial value
obj.setValue(50); // Modifies the member value
obj.display(); // Displays the updated value
return 0;
}
Output:
Member value: 10
Member value: 50
Explanation:
In the constructor and member function, both the parameter and member variable have the
same name (value).
The this pointer is used to refer to the member variable (this->value), so it avoids
ambiguity.
3. Using this in a Function Returning the Object Itself
You can return the current object from a function using the this pointer.
#include <iostream>
using namespace std;
class Example {
private:
int value;
public:
Example(int value) {
this->value = value;
}
// Function that returns the current object
Example& setValue(int value) {
this->value = value;
return *this; // Return the current object (dereferencing 'this')
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Example obj(100);
obj.display();
// Chaining method calls
obj.setValue(200).display(); // Modify the value and display it
immediately
return 0;
}
Output:
Value: 100
Value: 200
Explanation:
The setValue function modifies the member variable and returns the current object
(*this).
This allows for method chaining, where multiple member functions can be called in a single
statement.
Summary of this Pointer:
1. Automatic Availability: this is automatically available in non-static member functions.
2. Points to Current Object: It points to the object the member function was called on.
3. Use Cases:
o Differentiating between member variables and function parameters when they have
the same name.
o Returning the current object from a function to enable method chaining.
Let me know if you need further clarification or additional examples!
UNIT 1:
Object-oriented technology (OOT) is a software design model that uses
objects to represent data and instructions for working with that data. OOT is
used to create applications and programs.
Uml archoettcure:
https://www.javatpoint.com/uml-architecture