BIT153 ยท 2081 ยท 10 marks
How does structure differ with class?What are the uses of friend function and friend class?Explain the types of constructors.[1+3+6]
How does structure differ with class?What are the uses of friend function and friend class?Explain the types of constructors.[1+3+6]
Quick answer
In C++, struct and class are almost identical, with one key difference: To allow an external function to access private data of a class; Useful for operator overloading (e; To allow two classes to share data without making members public.
Structure vs Class, Friend Functions/Classes, and Types of Constructors
1. How does Structure differ with Class?
In C++, struct and class are almost identical, with one key difference:
| Feature | Structure (struct) | Class (class) |
|---|---|---|
| Default access specifier | Public | Private |
| Default inheritance | Public | Private |
| General use | Simple data grouping | Full OOP with data hiding |
Example:
struct S { int x; }; // x is public by default
class C { int x; }; // x is private by default
In C++, a
structcan also have member functions, constructors, and inheritance, just like a class.
2. Uses of Friend Function and Friend Class
Friend Function
A friend function is a non-member function that is granted access to the private and protected members of a class by declaring it with the keyword friend inside the class.
Uses:
- To allow an external function to access private data of a class
- Useful for operator overloading (e.g.,
<<,>>) - To allow two classes to share data without making members public
Syntax:
class Box {
private:
int length;
public:
Box(int l) { length = l; }
friend void showLength(Box b); // friend declaration
};
void showLength(Box b) {
cout << "Length: " << b.length; // accesses private member
}
Friend Class
A friend class is a class that is granted access to the private and protected members of another class.
Uses:
- When two closely related classes need to share private data
- Useful in implementing container and iterator relationships
Syntax:
class Engine {
private:
int horsepower;
public:
Engine(int hp) { horsepower = hp; }
friend class Car; // Car is a friend of Engine
};
class Car {
public:
void show(Engine e) {
cout << "HP: " << e.horsepower; // accesses private member
}
};
Note: Friendship is not inherited and not mutual (if A is a friend of B, B is not automatically a friend of A).
3. Types of Constructors
A constructor is a special member function that has the same name as the class, has no return type, and is automatically called when an object is created. It is used to initialize objects.
Type 1: Default Constructor
A constructor that takes no arguments (or all arguments have default values).
- Automatically provided by the compiler if no constructor is defined
- Called when an object is created without any arguments
class Student {
public:
int roll;
string name;
Student() { // Default constructor
roll = 0;
name = "Unknown";
}
};
int main() {
Student s; // Default constructor called
}
Type 2: Parameterized Constructor
A constructor that accepts one or more arguments to initialize an object with specific values.
- Allows different objects to be initialized with different values
- Supports constructor overloading
class Student {
public:
int roll;
string name;
Student(int r, string n) { // Parameterized constructor
roll = r;
name = n;
}
};
int main() {
Student s1(1, "Ram");
Student s2(2, "Sita");
}
Type 3: Copy Constructor
A constructor that creates a new object as a copy of an existing object. It takes a reference to an object of the same class as its parameter.
- Called when an object is initialized from another object
- Called when an object is passed by value to a function
- Called when an object is returned by value from a function
class Student {
public:
int roll;
string name;
Student(int r, string n) {
roll = r;
name = n;
}
Student(const Student &s) { // Copy constructor
roll = s.roll;
name = s.name;
}
};
int main() {
Student s1(1, "Ram");
Student s2(s1); // Copy constructor called
Student s3 = s1; // Copy constructor called
}
Summary Table
| Type | Arguments | Purpose |
|---|---|---|
| Default Constructor | None | Initializes object with default values |
| Parameterized Constructor | One or more | Initializes object with given values |
| Copy Constructor | Reference to same class object | Creates a copy of an existing object |
Key Points:
- A class can have multiple constructors (overloading)
- Constructors cannot be inherited but a derived class can call the base class constructor
- If any constructor is defined, the compiler does not provide a default constructor automatically
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.