Constructor/Desctructor Calling sequence:
• Constructors execute from the topmost base class down to the
most derived class
• Construction order depends strictly on the inheritance
hierarchy, not the initialization list order
• Destructors execute from the most derived class up to the base
class
• Destructors are called during stack unwinding when exceptions
occur
#include <iostream>
using namespace std;
class Human {
public:
Human() {
cout << "Human constructor called" << endl;
}
~Human() {
cout << "Human destructor called" << endl;
}
};
class Student : public Human {
public:
Student() {
cout << "Student constructor called" << endl;
}
~Student() {
cout << "Student destructor called" << endl;
}
};
class CSE_Student : public Student {
public:
CSE_Student() {
cout << "CSE_Student constructor called" << endl;
}
~CSE_Student() {
cout << "CSE_Student destructor called" << endl;
}
};
int main() {
cout << "Creating CSE_Student object..." << endl;
CSE_Student cs;
cout << "Object going out of scope..." << endl;
return 0;
}
Output :
Creating CSE_Student object...
Human constructor called
Student constructor called
CSE_Student constructor called
Object going out of scope...
CSE_Student destructor called
Student destructor called
Human destructor called
Object Slicing:
• occurs when a derived class object is assigned to a base class
object
• causing the derived class's additional attributes/methods to be
"sliced off".
• Only the base class part is copied, losing the derived class's
specialized features.
• From the above example:
CSE_Student cs;
Human h = cs // Object is sliced, only Human part is copied
• No object slicing occur when the object of derived class is
assigned to pointer of base class.
Human* h = new CSE_Student(); // No Object slicing
friend :
• In C++ friend is a keyword, used to give a non-member function or
another class access to the private and protected members of a class.
• This breaks the usual encapsulation, but it's sometimes useful when
two or more classes need to closely cooperate.
Types of friends in C++:
1. Friend Function
2. Friend Class
3. Friend Member Function (of another class)
Friend Function :
• A function that is not a member of the class but can access private/protected
data.
• They are external functions with special access privileges.
• Friendship is not mutual. If f() is a friend of A, A is not
automatically a friend of f()'s class (if any).
• Friend declarations can appear in private, protected, or public sections
(access specifiers don’t affect them).
• A single function can be a friend of multiple classes.
• A class can have multiple friends
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string secret; // Private member
public:
// Constructor using 'this' pointer
Student(string secret) {
this->secret = secret;
}
// Declare friend function
friend void revealSecret(Student s);
};
// Definition of friend function
void revealSecret(Student s) {
cout << "The student's secret is: " << s.secret << endl;
}
int main() {
Student s("pass@123"); // Object created with default secret "pass@123"
// Friend function accesses the private secret
revealSecret(s);
return 0;
}
Friend Class :
• When a class is declared as a friend, all its member functions can
access the private and protected members of the other class.
• Complete Access: All member functions of the friend class (B) can
access all private members of the original class (A)
• One-Way Relationship: Friendship is not mutual - A doesn't
automatically become a friend of B
• No Inheritance: Friendship doesn't imply inheritance
• Not Transitive: If B is a friend of A, and C is a friend
of B, C doesn't automatically become a friend of A
#include <iostream>
#include <string>
using namespace std;
// Forward declaration of B
class B;
class A {
private:
string secret; // Private member with default value
public:
// Constructor using 'this' pointer
Student(string secret) {
this->secret = secret;
}
// Declare entire class B as a friend
friend class B;
};
// Definition of B (after A is fully defined)
class B {
public:
// Function defined
void revealSecret(A a) {
cout << "B accesses A's secret: " << a.secret << endl;
}
};
int main() {
A a_obj("pass@123");
B b_obj;
b_obj.revealSecret(a_obj); // B accesses A's private member
return 0;
}
Friend Member Function (of another class) :
• Only a specific function from another class is given friend access.
• Friend member functions allow selective access (better than full
friend classes).
• Forward declaration of the friend's class is required.
#include <iostream>
#include <string>
using namespace std;
// Forward declaration
class B;
class A {
private:
string secret;
public:
// Constructor using 'this' to initialize secret
A(string secret) {
this->secret = secret;
}
// Declare specific member function of B as friend
friend void B::showSecret(A a);
};
class B {
public:
// Friend member function
void showSecret(A a) {
cout << "B accesses A's secret: " << a.secret << endl;
}
// Non-friend function (won't have access)
void noAccess(A a) {
// cout << a.secret; // This would cause error
}
};
int main() {
A a("pass@123"); // Object created with secret "pass@123"
B b;
b.showSecret(a); // Success - friend function
// b.noAccess(a); // Would fail - not a friend
return 0;
}
Comparison: Friend Function vs. Friend Member Function vs. Friend Class
Friend Member Friend Class
Feature Friend Function
Function
Single member All member
Standalone functions of
Access Scope function of another
function another class
class
friend void friend void friend class
Declaration ClassB;
func(); ClassB::func();
Required
Forward Decl. Not needed Required for ClassB for ClassB
Weakest- Any Weaker - All
external Moderate- Only a members of
function (even specific member another class get
Encapsulation access, but at
in global function of another
namespace) gets class gets access least it's scoped
access to a class