0% found this document useful (0 votes)
7 views

Lecture 04 OOP

this is oop lec 4

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 04 OOP

this is oop lec 4

Uploaded by

hi070125f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented Programming

(OOP)

Lecture 04

Engr. Dr. Ghulam Fareed Laghari

Department of Computer Science


Barani Institute of Management Sciences (BIMS)
Rawalpindi
CONTENT
 Constructors: Constructor Overloading

 Constructors: Default Copy Constructor

 Constructors: Default Copy Constructor (Example#1)

 Constructors: Default Copy Constructor (Example#2)

 Constructors: General Example#1

 Constructors: General Example#2


3

Constructors: Constructor Overloading


 Constructor overloading refers to declaring multiple constructors with the same name but different parameters.
 The constructors must differ in at least one of the following aspects:
 The number of parameters.
 The type of parameters.
 The sequence of parameters.
 Example 8:
class Rectangle { // Parameterized constructor
private: Rectangle(int l, int w) {
int length; length = l;
int width; width = w;
}
public: void display() {
// Default constructor cout << "Length: " << length << ", Width: " <<
Rectangle() { width << endl;
length = 0; }
width = 0; };
}
4

Constructors: Constructor Overloading


 Example 8:
int main() {
Rectangle rect1; // Calls default constructor OUTPUT
Rectangle rect2(10, 5); // Calls parameterized
constructor Length: 0, Width: 0
Length: 10, Width: 5
rect1.display();
rect2.display();

return 0;
}

 Code Explanation:

Rectangle(): Default constructor sets both length and width to 0.

Rectangle(int l, int w): Parameterized constructor allows initialization of length and width with given
values.
5

Constructors: Default Copy Constructor


 A default copy constructor initializes an object using another object of the same type.

 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:

class_name object_name(parameter); OR class_name object_name = parameter;

OR
class_name object_name(existing_object); OR class_name object_name = existing_object;
6

Constructors: Default Copy Constructor (Example#1)


 class_name: Refers to the name of the class and specifies the type of object to be instantiated.

 object_name: Denotes the name of the object being created.

 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

Constructors: Default Copy Constructor (Example#1)


 Example 9:
 CODE EXPLANATION:
1. Class Definition:
The MyClass class is defined with a public integer member variable called value.
The constructor MyClass(int v) initializes value with the value passed as an argument using an initializer list.

2. Creating obj1:
In main(), MyClass obj1(10); creates an instance of MyClass named obj1 and initializes its value member to 10.

3. Copying obj1 to obj2:


The line MyClass obj2 = obj1; invokes the default copy constructor, which copies the value from obj1 to obj2.
As a result, obj2.value also becomes 10.

4. Output:
Finally, cout << obj2.value; prints the value of obj2, which is 10.
8

Constructors: Default Copy Constructor (Example#2)


 Example 10: If you have a class called Rectangle, the copy constructor will allow you to create a new Rectangle
object using an already existing Rectangle object.
class Rectangle { int main() {
public: Rectangle rect1(10, 5); // Creates rect1
int width, height; Rectangle rect2 = rect1; // Uses the copy constructor to
// Constructor create rect2 using rect1
Rectangle(int w, int h) : width(w), height(h) {} cout << "Width: " << rect2.width << ", Height: " <<
// Copy Constructor rect2.height; // Output: Width: 10, Height: 5
Rectangle(const Rectangle &rect) { return 0;
width = rect.width; }
height = rect.height;
}
};
9

Constructors: Default Copy Constructor (Example#2)


 Example 10: If you have a class called Rectangle, the copy constructor will allow you to create a new…

 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) : width(w), height(h) {} is a constructor:

 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

Constructors: Default Copy Constructor (Example#2)


 Example 10: If you have a class called Rectangle, the copy constructor will allow you to create a new…

 CODE EXPLANATION: Alternates

// Constructor // Copy Constructor


Rectangle(const Rectangle &rect) {
Rectangle(int w, int h) : width(w), height(h) {}
width = rect.width;
ALTERNATE:
height = rect.height; }
Rectangle(int w, int h) { ALTERNATE:
width = w; 1. Rectangle(const Rectangle &rect) : width(rect.width),
height(rect.height) {}
height = h;
2. Rectangle(Rectangle rect) { // Parameter passed by value
}
width = rect.width; // Copy values from the parameter
height = rect.height; }
However, this approach (ALTERNATE#2) will create a copy of the Rectangle object that is
passed to the constructor, which can be less efficient, especially for larger objects.
11

Constructors: General Example#1


 Example 11: Student Records Management: Implement a simple program to manage and display student records, including their marks and grades.

class Student { void main() {


private: Student s1(980, 'A'), s2(913, 'B');
int marks;
char grade; cout << "Record of Student 1:" << endl;
public: // Constructor s1.show();
Student(int m, char g) {
cout << "Record of Student 2:" << endl;
marks = m;
s2.show();
OUTPUT
grade = g; }
Record of Student 1:
void show() {
return 0; Marks = 980
cout << "Marks = " << marks << endl; Grade = A
}
cout << "Grade = " << grade << endl; } Record of Student 2:

}; Marks = 913
Grade = B
12

Constructors: General Example#1


 Example 11: Student Records Management: Implement a simple program to manage and display student records, including their marks and grades.

 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.

Displays the records of both students by calling the show function.


13

Constructors: General Example#2


 Example 12: Create a class named Book that stores the title, pages, and price of a book. The program should allow users to input the
details of a book, create a copy of the book object, and display the details of all book objects.
class Book { void main() {
private: Book b1;
int pg, pr; b1.get();
char title[50]; Book b2(b1);
public: Book b3;
void get() {
cout << "Enter title: "; cout << "\nThe detail of b1:" << endl;
std::getline(std::cin, title); b1.show();
cout << "Enter pages: "; cout << "\nThe detail of b2:" << endl;
cin >> pg; b2.show();
cout << "Enter price: "; cout << "\nThe detail of b3:" << endl;
cin >> pr; } b3.show();
void show() {
cout << "Title: " << title << endl; return 0; }
cout << "Pages: " << pg << endl;
cout << "Price: " << pr << endl; } };
14

Constructors: General Example#2


 Example 12: Create a class named Book that stores the title, pages, and price of a book. The program should allow users…
Buffer overflows occur when a program attempts to write
 CODE EXPLANATION: more data to a fixed-size buffer than it can hold. This can
lead to various security vulnerabilities and unexpected
 char title[50]; declares a character array named title with a maximum length of 50 characters. program behavior. Use prevention techniques, e.g.,
std::string
 std::getline() function from the C++ standard library: This function allows to specify the maximum number of characters to read and handles buffer
overflow issues more effectively. When we use std::getline(std::cin, title);, the input string is stored in this array. If the input string is longer than 50
characters, only the first 50 characters will be stored, and the rest will be truncated.

 Class Declaration: Defines a class named Book to represent book objects.

 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

You might also like