Lecture 04 OOP
Lecture 04 OOP
(OOP)
Lecture 04
return 0;
}
Code Explanation:
Rectangle(int l, int w): Parameterized constructor allows initialization of length and width with given
values.
5
This constructor is automatically provided by default in all classes, so the user does not need to explicitly define it.
It takes a single object of the same class type as its parameter. [i.e., the copy constructor accepts an existing object of
the same class as an argument. This allows the new object to be initialized with the values or state of the existing object.]
The parameter can be passed either within parentheses or by using the assignment operator (i.e., =).
SYNTAX: The syntax for utilizing the default copy constructor is provided below:
OR
class_name object_name(existing_object); OR class_name object_name = existing_object;
6
parameter: Represents the parameter passed to the default copy constructor, where the data members of the parameter
object are copied to the new object. Both the parameter object and the new object must be of the same class type.
Example 9:
class MyClass { int main() {
public: MyClass obj1(10); // Creates obj1 with value = 10
int value; MyClass obj2 = obj1; // Uses the default copy constructor,
copies obj1 to obj2
MyClass(int v) : value(v) {} //OR MyClass(int v) { value=v; }
}; cout << obj2.value; // Output: 10
return 0;
}
7
2. Creating obj1:
In main(), MyClass obj1(10); creates an instance of MyClass named obj1 and initializes its value member to 10.
4. Output:
Finally, cout << obj2.value; prints the value of obj2, which is 10.
8
CODE EXPLANATION:
Rectangle(const Rectangle &rect) is the copy constructor. It takes a single object (rect1) of type Rectangle (the same class type) as its parameter to initialize
the new object rect2. This means the new object (rect2) will have the same width and height values as the original (rect1).
const: The const keyword indicates that the object rect passed to the copy constructor cannot be modified within the constructor. This means that you are
promising not to change rect while you are using it to initialize the new object. It helps prevent accidental changes to the original object.
Rectangle: This is the type of the parameter. The parameter rect is an object of the Rectangle class.
& (Reference):The ampersand (&) indicates that rect is a reference to an existing Rectangle object, rather than a copy of it. By using a reference, you avoid
making a copy of the rect object when the constructor is called. This is more efficient because it prevents unnecessary copying of potentially large objects.
Rectangle(int w, int h) is the constructor, which takes two parameters: w (for width) and h (for height).
: width(w), height(h) is the initializer list, which directly assigns the values w and h to the class data members width and height before the constructor
body executes.
10
}; Marks = 913
Grade = B
12
Code Explanation:
1. Class Declaration: The Student class is defined with private attributes marks and grade.
2. Constructor: A constructor function is defined to initialize the attributes when a new object is created.
3. Show Function: The show function displays the values of marks and grade.
4. Main Function:
Creates two Student objects (s1 and s2) with specific values.
Private Members: int pg: Stores the number of pages in the book. int pr: Stores the price of the book. char title[50]: Stores the title of the book as a
character array with a maximum length of 50 characters.
Public Member Functions: void get(): Prompts the user to enter the title, pages, and price of a book. Reads the input from the user and stores it in the
corresponding private member variables. void show(): Displays the title, pages, and price of the book object.
Main Function: Creates three Book objects: b1, b2, and b3. Calls the get() function on b1 to get the user's input. Creates a copy of b1 and assigns it to
b2 using the copy constructor. Calls the show() function on b1, b2, and b3 to display their details.
THANK YOU
15