Beautiful - Ai - COMP116 - Chapter
Beautiful - Ai - COMP116 - Chapter
Saugat Singh
Visiting Lecturer
Topics of Discussion
4 Initializing an Object
2
Thought !!!
The program should store relevant information of student and teacher and display them.
Procedural approach
4
#include <stdio.h>
Remember the points. We will need them till the end of the slides.
7
1. We can pass wrong data to function
#include <stdio.h> int main() {
struct Student {
struct Student student1 = {1, "Alice"};
int roll;
char name[50]; struct Teacher teacher1 = {"Mr. Smith",
}; 123456789};
struct Teacher{
displayStudent(teacher1);
char name[50];
int phone; displayTeacher(student1);
};
return 0;
void displayStudent(struct Student s) {
}
printf("Student Roll Number: %d\n",
s.roll);
printf("Student Name: %s\n", s.name);
} Here,
void displayTeacher(struct Teacher t) { we have passed teacher1 to displayStudent()
function which expects structure Student.
printf("Teacher Name: %s\n", t.name);
printf("Teacher Phone Number: %d\n", So, we will have a compilation error.
t.phone);
}
8
Preventing the mistake 1
Solution 1: Using Function overloading and reference variables
9
#include <iostream> int main() {
using namespace std;
struct Student student1 = {1, "Alice"};
struct Student { struct Teacher teacher1 = {"Mr. Smith",
int roll; 123456789};
char name[50];
}; display(student1);
cout << endl;
struct Teacher { display(teacher1);
char name[50];
int phone; return 0;
}; }
void display(Student& s) {
cout << "Student Roll Number: " << s.roll << endl;
cout << "Student Name: " << s.name << endl;
}
void display(Teacher& t) {
Here,
cout << "Teacher Name: " << t.name << endl; We overload the function to accept both
cout << "Teacher Phone Number: " << t.phone << types of structures and print relevant
endl;
information.
}
10
Preventing the mistake 1
Solution 2: Binding the data with functions
Class Student
Class Teacher
11
#include <iostream>
#include <cstring> int main() {
using namespace std; Student student1;
Teacher teacher1;
class Student {
public: student1.roll = 1;
int roll; strcpy(student1.name, "Alice");
char name[50];
teacher1.phone = 123456789;
void display() { strcpy(teacher1.name, "Mr. Smith");
cout << "Student Roll Number: " << roll <<
endl; student1.display();
cout << "Student Name: " << name << endl; cout << endl;
} teacher1.display();
};
return 0;
class Teacher { }
public:
char name[50];
int phone;
Here,
void display() {
student1.display() will execute function
cout << "Teacher Name: " << name << endl;
cout << "Teacher Phone Number: " << phone <<
inside the Student class.
endl; teacher1.display() will execute function inside
} the Teacher class
}; 12
Finally Back to the topic
Introduction to Class and Objects
14
Dissecting our first program
class Student {
public: 4. Access Modifier
int roll;
5. Attributes int marks;
void displayInformation() {
cout << "Student Information" <<
endl;
cout << "Roll Number: " << roll
3. Class <<endl;
cout << "Marks: " << marks;
6. Methods
}
void readInformation() {
cout << "Enter Roll Number: ";
cin >> roll;
cout << "Enter Marks: ";
cin >> marks;
}
};
15
#include <iostream> int main() {
using namespace std; Student student1, student2;
class Student {
public: student1.readInformation();
int roll; student1.displayInformation();
int marks;
student2.roll = 2;
void displayInformation() { student2.marks = 100;
cout << "Student Information" << student2.displayInformation();
endl;
cout << "Roll Number: " << roll return 0;
<<endl; }
cout << "Marks: " << marks;
}
void readInformation() {
cout << "Enter Roll Number: ";
cin >> roll;
cout << "Enter Marks: ";
cin >> marks;
}
}; 16
Access Modifiers
17
Remember, our mistake 2?
"The data are accessible to all."
20
Defining a class with member funtion
class ClassName {
public:
Functions defined within a class to
return_type functionName()
manipulate data members.
{
Member functions are used to perform
// Function body
operations on the data members of the
}
class, which are the variables defined
};
inside the class.
class ClassName {
public: Function is declared inside the class and
return_type functionName(); defined outside using scope resolution
}; operator (::)
21
#include <iostream> // Function definition
using namespace std; void Student::displayInformation() {
cout << "Student Information" << endl;
class Student { cout << "Roll Number: " << roll <<
private: endl;
int roll; cout << "Marks: " << marks << endl;
int marks; }
Encapsulation Abstraction
Bundling of data and methods that operate on Concept of hiding the complex implementation
that data within a single unit, i.e., a class. details and showing only the essential features of
It restricts direct access to some of the object's the object.
components Focus on What an Object Does: Not how it does
Data Hiding: Protects the internal state of the it.
object from unintended or harmful changes. Hides implementation details.
Access Modifiers: Uses private, protected, and Focuses on exposing only the necessary aspects
public to control access.
Benefits?
24
Benefit
Modularity Reusability
The code is divided into separate objects that Code can be reused through inheritance,
encapsulate their own data and operations. allowing new classes to use existing
functionality.
Scalability Maintainability
OOP allows for the easy expansion and Enhances code maintainability and
scaling of complex applications by adding readability by organizing related properties
new objects and relationships. and behaviors into single entities (objects).
25
Static Data Members and Methods
allow sharing data and behavior across all instances of a class.
Definition: A static data member is a variable that is Definition: static method is a function that belongs to the
shared by all objects of a class. Only one copy of the static class rather than any object instance. It can access only
data member exists, regardless of how many objects are static data members and other static methods.
created from the class.
Characteristics:
Characteristics: Declared using the static keyword.
Declared using the static keyword. Can be called on the class itself, not on objects of the
Initialized outside the class definition. class.
Shared among all instances of the class.
26