Unit 2
Unit 2
Placements
Welcome to Turning Point by Ankush Saklecha, your one-stop destination for GATE CSE
preparation, semester exams, and placement guidance. Stay connected with us across various
platforms for expert mentorship, study materials, and career guidance.
�Get everything you need to excel in academics and secure top placements!
�Join our Telegram Group for interactive discussions and quick solutions to your doubts.
�Follow us on Instagram, LinkedIn, and YouTube for exclusive content and live updates.
�Prepare for GATE, semester exams, and placements with the best resources. Stay connected,
stay ahead!
Unit II
OOP languages like C++, Java, and Python use I/O streams to handle input and
output efficiently.
Q2. What are I/O Streams?
Definition:
IO Streams in C++
iostream:
Inherits from both istream and ostream for simultaneous input and
output.
b) Commonly Used Objects:
IO Streams in Java
Java's IO system is based on streams as well, but it uses a different class hierarchy
found in the java.io package.
Byte Streams:
Character Streams:
1. State (Attributes/Data)
2. Behavior (Methods/Functions)
3. Identity (Unique Existence)
State of an Object
The state of an object refers to the data or attributes that define its current
condition. The state changes when the values of its attributes are modified.
class Car {
public:
string brand;
string color;
int speed;
};
Changing State:
Car c1;
c1.brand = "Toyota";
c1.color = "Red";
c1.speed = 60;
Now, c1 has the state: Brand = Toyota, Color = Red, Speed = 60.
Behavior of an Object
The behavior of an object refers to the methods (functions) that define what
actions the object can perform.
Example:
Continuing with the Car class, we can define behaviors like accelerate() and
brake().
class Car {
public:
string brand;
int speed;
Identity of an Object
Definition:
Car car1;
Car car2;
Even if car1 and car2 have the same state (brand, color, speed), they are different
objects stored at different memory locations.
#include <iostream>
using namespace std;
class Car {
public:
string brand;
Car(string b) { brand = b; }
};
int main() {
Car car1("Toyota");
Car car2("Toyota");
cout << "Car1 Memory Address: " << &car1 << endl;
cout << "Car2 Memory Address: " << &car2 << endl;
return 0;
}
1. Class:
Represents a blueprint for objects.
Depicted as a rectangle with three compartments:
1. Class Name (at the top)
2. Attributes (in the middle)
3. Methods/Operations (at the bottom)
2. Attributes (Properties/Fields):
Define the characteristics of a class.
Example: name, age, salary in a Person class.
3. Methods (Operations/Functions):
Define the behavior of a class.
Example: getSalary(), setAge() in a Person class.
4. Access Specifiers (Visibility Modifiers):
o Control access to attributes and methods:
+ (Public) → Accessible everywhere
- (Private) → Accessible only within the class
# (Protected) → Accessible within the class and its
subclasses
5. Relationships:
Association (↔) → A relationship between two classes (e.g., a
"Teacher teaches a Student").
Multiplicity (1..*, 0..1) → Defines the number of instances (e.g., one
teacher can teach multiple students).
Aggregation (○--) → Weak relationship (e.g., A "Department"
consists of multiple "Professors").
Composition (◆--) → Strong relationship (e.g., A "Car" has an
"Engine" that cannot exist separately).
Inheritance (Generalization) (▸|—) → Represents parent-child
relationships.
Q5. What is Association? Explain the various
forms of Association.
Association in UML Class Diagrams
Forms of Association
int main() {
Engine eng; // Engine exists independently
Car myCar(&eng); // Aggregation: Car uses Engine but doesn’t own it
myCar.startCar(); // Car starts using the Engine
return 0;
}
#include <iostream>
using namespace std;
class Person {
private:
string name; // Private: Not accessible outside the class
protected:
int age; // Protected: Accessible in derived classes
public:
void setName(string n) { name = n; } // Public function
void display() { cout << "Name: " << name << ", Age: " << age << endl; }
};
// Derived Class
class Student : public Person {
public:
void setAge(int a) { age = a; } // Accessing protected member
};
int main() {
Student s;
s.setName("John"); // Allowed (public method)
s.setAge(20); // Allowed (protected variable accessible in derived class)
s.display();
return 0;
}
Key Features:
#include <iostream>
using namespace std;
class Student {
private:
static int count; // Static data member (shared by all objects)
public:
Student() { count++; } // Increment count when a new object is created
static void showCount() { // Static function to access static member
cout << "Total Students: " << count << endl;
}
};
// Definition of static member outside the class
int Student::count = 0;
int main() {
Student s1, s2, s3;
Student::showCount(); // Accessing static function
return 0;
}
A static member function is a function that can access only static data members.
It does not operate on object-specific data.
Key Features:
#include <iostream>
using namespace std;
class Counter {
private:
static int value; // Static data member
public:
static void increment() { // Static function
value++;
cout << "Counter Value: " << value << endl;
}
};
// Define static member outside class
int Counter::value = 0;
int main() {
Counter::increment(); // Calling static function without object
Counter::increment();
return 0;
}
1. Definition:
Aggregation: It is a type of association where one object "has a"
relationship with another object. It represents a "whole-part" relationship
where the part can exist independently of the whole. It is a more flexible,
loosely coupled relationship compared to inheritance.
Inheritance: It represents an "is-a" relationship where a subclass inherits the
properties and behaviors of a superclass. Inheritance is tightly coupled
because the subclass depends on the superclass for its functionality.
2. Type of Relationship:
Aggregation: A "has-a" relationship. For example, a Car has an Engine.
The Engine can exist without the Car.
Inheritance: An "is-a" relationship. For example, a Dog is a Mammal. The
Dog inherits all attributes and behaviors of the Mammal.
3. Dependency:
Aggregation: The lifetime of the part does not depend on the lifetime of the
whole. For example, if a Team object contains multiple Player objects, the
Player objects can exist independently of the Team.
Inheritance: The lifetime of the subclass is dependent on the superclass. If a
Dog is a subclass of Animal, the Dog cannot exist without the concept of
Animal.
4. Reusability:
Properties of Aggregation:
1. "Has-a" Relationship:
Aggregation expresses a "whole-part" relationship where the whole
can exist independently of the parts. For example, a Library has
many Books, but the Books can exist even without the Library.
2. Independent Existence:
The objects involved in aggregation can exist independently of each
other. For instance, a Department can contain many Employee
objects, but the Employees exist independently and are not
necessarily bound to a specific Department.
3. No Ownership:
The whole does not own the part. In aggregation, a part can be shared
by multiple objects. For example, multiple Cars can use the same
Engine.
4. Loose Coupling:
Aggregation represents a loose coupling between the whole and part
objects, meaning they can interact but are not strongly dependent on
one another.
5. Life Cycle Independence:
The lifespan of the part object is not dependent on the lifespan of the
whole. When the whole object is destroyed, the part object can still
exist.
6. Refers to Collection:
Aggregation typically represents a collection of objects, where one
object contains a set of other objects, often with a common theme. For
example, a Team can aggregate many Players, but the Players are
not tightly bound to the Team.