0% found this document useful (0 votes)
13 views26 pages

Beautiful - Ai - COMP116 - Chapter

Uploaded by

Topendra Bandhan
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)
13 views26 pages

Beautiful - Ai - COMP116 - Chapter

Uploaded by

Topendra Bandhan
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/ 26

COMP 116

Object Oriented Programming


June 2024

Saugat Singh
Visiting Lecturer
Topics of Discussion

Chapter 3 Classes and Objects

1 Introduction to class and objects

2 Defining a class with member function

3 Private member function

4 Initializing an Object

5 Static Data Members

6 Static Member Functions

2
Thought !!!

Suppose, we want to have a program, that keeps the record of


students and teachers.

The program should store relevant information of student and teacher and display them.
Procedural approach

Create structures to store the Create functions to display the


Main function
information information

1. Structure called "Student" to 1. Create function 1. Create a variable called


store student info "displayStudent()" to display student1 to store the
the student info information of first student.
2. Structure called "Teacher" to
display the teacher info 2. Create function 2. Pass the information to
"displayTeacher()" to display displayStudent() function to
the teacher info print.
3. Create a variable called
teacher1 to store the
information of one teacher.
4. Pass the information to
displayTeacher() function to
print.

4
#include <stdio.h>

struct Student { int main() {


int roll;
char name[50]; struct Student student1 = {1,
}; "Alice"};
struct Teacher{
char name[50]; struct Teacher teacher1 = {"Mr.
int phone; Smith", 123456789};
};
displayStudent(student1);
void displayStudent(struct Student s) {
printf("Student Roll Number: %d\n", displayTeacher(teacher1);
s.roll);
printf("Student Name: %s\n", s.name); return 0;
} }
void displayTeacher(struct Teacher t) {
printf("Teacher Name: %s\n", t.name);
printf("Teacher Phone Number: %d\n",
t.phone);
} 5
Question !!!

What could be the potential mistakes we can make?


Mistakes we can make?

1 We can pass wrong data 2 The data are accessible to


to function. all.

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

Structure "Student" display(Student)

Structure "Teacher" display(Teacher)

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

Structure "Student" display()

Class Teacher

Structure "Teacher" display()

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

What is a Class? Components:


A blueprint for creating objects. Attributes (Data Members): Variables to hold data.
Contains data members (attributes) and member Methods (Member Functions): Functions to
functions (methods). manipulate the data.
Does not occupy memory space.

Definition of Object: Syntax:


An instance of a class. Variable of class datatype. Class: class ClassName {
Each object has its own copy of the data members data members;
defined in the class and can use the member member functions; };
functions to interact with these data members. Object: ClassName objectname;

14
Dissecting our first program

1. Header file #include <iostream>


using namespace std; 2. Namespace

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

What are access modifiers? Types of Access Modifiers:


keywords used to control the accessibility of class 1. Public
members 2. Private
Used extensively for hiding the data. 3. Protected
Will affect next lines of codes.

Public: Private: Protected:


can be accessed by any part of accessible only within the accessible within the class and
the program. class that defines them. by derived classes.

17
Remember, our mistake 2?
"The data are accessible to all."

Utilizing the mistake, let us hack our program.


2. The data are accessible to all
#include <iostream> int main() {
using namespace std; Student student1, student2;
class Student {
public: student1.readInformation();
int roll; student1.displayInformation();
int marks;
student2.readInformation();
void displayInformation() { student2.marks = 100;
cout << "Student Information" << student2.displayInformation();
endl;
cout << "Roll Number: " << roll return 0;
<<endl; }
cout << "Marks: " << marks;
}
void readInformation() {
Here, I changed the marks of student 2 after
cout << "Enter Roll Number: ";
taking the input from user. This is a grave
cin >> roll;
concern, we should not let anyone tamper
cout << "Enter Marks: ";
with the marks.
cin >> marks;
} 19
Preventing the mistake 2.
Controlling who can run the code.

#include <iostream> int main() {


using namespace std; Student student1, student2;
class Student {
private: student1.readInformation();
int roll; student1.displayInformation();
int marks;
public: student2.readInformation();
void displayInformation() { // the next line will produce error.
cout << "Student Information" << endl; student2.marks = 100;
cout << "Roll Number: " << roll <<endl;
cout << "Marks: " << marks; student2.displayInformation();
}
void readInformation() { return 0;
cout << "Enter Roll Number: "; }
cin >> roll;
cout << "Enter Marks: ";
cin >> marks; Here, I implemented, access modifier, to the
}
data members, so that they are not accessible
};
from outside the class.

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 (::)

return_type ClassName ::functionName()


{
// Function body
}

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; }

public: int main() {


// Function declaration Student student1;
void displayInformation(); student1.readInformation();
student1.displayInformation();
void readInformation() {
cout << "Enter Roll Number: "; return 0;
cin >> roll; }
cout << "Enter Marks: ";
cin >> marks;
}
};
#include <iostream> // Function definition
using namespace std; void Student::displayInformation() {
class Student { cout << "Student Information" << endl;
private: cout << "Roll Number: " << roll <<
int roll; endl;
int marks; cout << "Marks: " << marks << endl;
void readInformation() { }
cout << "Enter Roll Number: ";
cin >> roll; int main() {
cout << "Enter Marks: "; Student student1;
cin >> marks; student1.read();
} // Accessing the private function is not
public: allowed
// Function declaration student1.readInformation();
void displayInformation(); student1.displayInformation();
void read(){
readInformation(); return 0;
} }
};
Revision

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.

Static Data Member Static Method

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

You might also like