Relationship between classes in C++
Object Oriented Programming (OOP)
The term ‘‘Object Oriented Programming” means a programming
language that fully supports the object oriented (features) style of
programming.
Introduction
C++ is an object-oriented programming language developed by Bjarne
Stroustrup at AT & T Bell Labs in the early 1980’s.
Since the class concept was the major addition to the original C language, it is called
as "C with classes". However later in 1983, the name was changed to C++. The idea
of C++ comes from the 'C' increment operator "++", there by suggesting that C++ is
an incremented version of 'C'.
C++ is a superset of C. Therefore almost all C programs are valid C++ programs.
Advantages of OOP
1. Object Oriented Approach
2. Closer to real world object
3. Dynamic Declaration
4. Data Security
5. Code Re-Usability & Extensibility
Basic concepts of ‘Object-Oriented’ programming language
Class
A class is a blueprint or prototype that defines the variables and the methods common
to all objects of a certain kind.
A class is a logical construct of an object.
A class is an abstract representation of something, whereas an object is a usable
example of the class.
A class instead of holding only data; it can hold both data and functions. Once a class
has been defined, you can create any number of objects based on that class.
Object
An object is an instance (example or illustration) of a class. In terms of
variables, a class would be the data type and an object would be the variable.
An object is a physical reality.
An object is a software bundle of related variables and methods.
Software objects are often used to model real-world objects you find in
everyday life.
Program:
#include <iostream>
using namespace std;
// Define a class named Student
class Student {
public:
// Data members (variables)
string name;
int rollNumber;
float marks;
// Member function to input student data
void inputData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNumber;
cout << "Enter marks: ";
cin >> marks;
}
// Member function to display student data
void displayData() {
cout << "\nStudent Details:" << endl;
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Marks: " << marks << endl;
}
};
// Main function
int main() {
// Create object of class Student
Student s1;
// Call member functions using object
s1.inputData();
s1.displayData();
return 0;
}
1. What is a Class Diagram?
A Class Diagram is a static structure diagram used in UML (Unified Modeling
Language) that shows:
Classes in the system
Attributes (data members) of each class
Methods (functions/behaviors)
Relationships among classes (Association, Inheritance, Aggregation,
Composition)
2. What is an Association (Uses) Relationship?
Association: A structural relationship that represents how objects of one class are
connected to objects of another.
A simple form of association is the "uses" relationship.
It shows that one class uses the services/functions of another class.
UML Notation: A solid line between two classes
Example Label: uses, works with, manages
Association
represents a "uses-a" relationship, where one class simply uses or refers to another.
Both classes are independent and can exist separately. For example, a Car class may
use an Engine class to start the engine, but the engine is not a permanent part of the
car—it just gets used when needed.
Aggregation
is a "has-a" relationship, where one class contains a reference to another class, but still
does not own it completely. The contained object (like an Engine in a Car) is passed
from outside and can exist independently. It shows a weak ownership. For instance, a
University has Professors, but those professors can exist even if the university is
deleted.
3. Example Problem Statement
"A Teacher teaches Students. A Student can enroll in multiple Courses. The Teacher
can assign Grades to Students in a Course."
4. Identifying Classes from Problem Statement
Entity Class Name
Teacher Teacher
Student Student
Course Course
Grade Grade
5. Identifying Association (Uses) Relationships
Teacher → Student: uses relationship (Teacher teaches Student)
Student → Course: uses relationship (Student enrolls in Course)
Teacher → Grade: uses relationship (Teacher assigns Grade)
Grade → Student & Course: uses (Grade is given to Student for a Course)
Relationship between classes:
1. Polymorphism (many forms)
An ability to have more than one function with the same name, each having
different functionality. The function going to execute is determined at run time.
Polymorphism refers to the fact that a single operation can have different behavior in
different objects.
E.g.: Overloading, Overriding.
Overloading:
Overloading is the practice of supplying more than one
definition for a given function name in the same scope. The compiler is left to pick
the appropriate version of the function or operator based on the arguments with which
it is called.
2. Inheritance
The mechanism of creating new classes by deriving the features from existing
classes is called Inheritance.
In OOP the concept of inheritance provides the idea of reusability & extensibility.
This means that we can add additional features of an existing class without modifying
it. This is possible by deriving a new class from the existing one. The new class will
have the combined features of both the classes.
3. Encapsulation
The method of packing all the data members & member functions into a single
unit called class and the process is called ‘Encapsulation’
Data Hiding
The process of hiding the data members and member functions
from access by the other class objects other than the same class objects.
The data is not accessible to the outside world and only those functions which are
placed in the class can access it, these functions provide the interface between objects
data and the program. The insulation of data from direct access by the program is
called data hiding.
4. Data Abstraction
It is the process of accessing the data members & member functions with the
objects.
Abstraction refers to the act of representing essential features without including the
background details and explanations. Classes use the concept of abstraction and
defined as a list of abstraction attributes. Since the classes use the concept of data
abstraction they are known as Abstract Data Type (ADT).
5. Data Binding
Binding refers to the act of associating an object or a class with its member. If
we can call a method fn() on an object o of a class c, we say that the object o is binded
with the method fn(). This happens at compile time and is known as static or compile
- time binding.
The calls to the virtual member functions are resolved during run-time. This
mechanism is known as dynamic binding. The most prominent reason why a virtual
function will be used is to have a different functionality in the derived class. The
difference between a non-virtual member function and a virtual member function is
the non-virtual member functions are resolved at compile time.
Syntax for Class Declaration
class class_name
{
access_specifier_1:
data members;
access_specifier_2:
member functions;
……….
} object_names;