Unit 6 - Notes
Unit 6: Derived types including structures and unions
1. Structures in C
A Structure is a user-defined data type in C/C++ that allows you to combine data items of different kinds. While arrays hold multiple elements of the same type, structures hold elements of different types.
Declaration of a Structure
The struct keyword is used to declare a structure. The variables declared inside the structure are called members or fields.
struct Student {
int roll_no; // 4 bytes
char name[20]; // 20 bytes
float marks; // 4 bytes
};
Definition and Initialization
Declaring the structure blueprint does not allocate memory. Memory is allocated when a structure variable (object) is defined.
Definition:
struct Student s1, s2; // Allocates memory for s1 and s2
Initialization:
You can initialize a structure at the time of declaration or later.
// Method 1: Compile-time initialization
struct Student s1 = {101, "Alice", 92.5};
// Method 2: Designated initializers (C99 standard)
struct Student s2 = {.marks = 88.0, .roll_no = 102, .name = "Bob"};

Accessing Structure Members
The Member Access Operator (dot operator .) is used to access individual members of a structure.
s1.roll_no = 105;
printf("Name: %s", s1.name);
2. Structures and Pointers
We can create a pointer that points to a structure variable. This is useful for dynamic memory allocation and passing structures to functions efficiently.
Declaration and Access
When accessing a member through a pointer, we cannot use the dot operator directly because of operator precedence. We use the Arrow Operator (->).
struct Student s1 = {101, "Alice", 90.5};
struct Student *ptr = &s1;
// Accessing members
printf("%d", (*ptr).roll_no); // Method 1: Dereference then dot (cumbersome)
printf("%d", ptr->roll_no); // Method 2: Arrow operator (preferred)
3. Nested Structures
A structure can contain another structure as one of its members. This is called nesting.
struct Date {
int day;
int month;
int year;
};
struct Employee {
int id;
char name[20];
struct Date dob; // Nested Structure
};
int main() {
struct Employee e1;
e1.id = 1001;
e1.dob.day = 15; // Accessing nested member
e1.dob.month = 8;
e1.dob.year = 1995;
}
4. Unions
A Union is similar to a structure, but with a critical difference in memory allocation. In a structure, each member has its own storage location. In a union, all members share the same memory location.
Declaration of a Union
union Data {
int i;
float f;
char str[20];
};
Memory Allocation Difference
- Size of Union: Equal to the size of its largest member. In the example above,
str[20]is the largest, so the union size is 20 bytes. - Behavior: You can only use one member at a time. Assigning a value to one member overwrites the values of others.

5. Concepts and Basics of C++ Programming
C++ extends C by adding Object-Oriented Programming (OOP) features.
Key Concepts
- Classes & Objects: The building blocks of OOP.
- Encapsulation: Wrapping data and functions into a single unit (Class).
- Abstraction: Hiding internal details and showing only functionality.
- Inheritance: Creating new classes from existing ones.
- Polymorphism: Ability to take multiple forms (e.g., function overloading).
Reading and Writing Data (cin and cout)
C++ uses the iostream library.
- cout (Character OUTput): Used with the insertion operator
<<to print to the console. - cin (Character INput): Used with the extraction operator
>>to take input.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: "; // Writing to console
cin >> age; // Reading from keyboard
cout << "You are " << age << " years old." << endl;
return 0;
}
6. Classes and Objects in C++
Creating Classes
A class is a blueprint for objects. It allows data hiding via access specifiers (public, private, protected). By default, members are private.
class Rectangle {
private:
int length;
int width;
public:
// Member function to set data
void setDimensions(int l, int w) {
length = l;
width = w;
}
// Member function to calculate area
int area() {
return length * width;
}
};
Class Objects and Accessing Members
int main() {
Rectangle rect1; // Creating an object
// rect1.length = 10; // ERROR: length is private
rect1.setDimensions(5, 10); // Accessing public member function
cout << "Area: " << rect1.area();
return 0;
}
7. Inline and Non-inline Member Functions
Inline Functions
A function defined inside the class definition is implicitly inline. Alternatively, the inline keyword can be used. The compiler replaces the function call with the actual code to save function calling overhead (context switching).
class Demo {
public:
void display() { // Implicitly Inline
cout << "Hello";
}
};
Non-inline Functions
Functions defined outside the class using the Scope Resolution Operator (::). These are treated as normal functions.
class Demo {
public:
void display(); // Declaration inside
};
// Definition outside
void Demo::display() {
cout << "Hello";
}
8. Static Data Members and Member Functions
Static Data Members
A variable declared as static inside a class is a Class Variable.
- Only one copy exists for the entire class, shared by all objects.
- It must be defined outside the class.
Static Member Functions
- Can only access static data members and other static member functions.
- Called using the class name, not an object.
class Counter {
static int count; // Static declaration
public:
static void showCount() { // Static function
cout << "Count: " << count << endl;
}
};
int Counter::count = 0; // Definition outside class
int main() {
Counter::showCount(); // Calling without object
}
9. Differences: Structures, Unions, Enumerations, Classes
| Feature | Structure (struct) |
Union (union) |
Enumeration (enum) |
Class (class) |
|---|---|---|---|---|
| Concept | Group of diff. data types. | Group sharing memory. | List of named integer constants. | Blueprint for objects (Data + Methods). |
| Memory | Sum of all members. | Size of largest member. | Size of an integer. | Depends on data members. |
| Access | All members active simultaneously. | Only one member active at a time. | Holds one constant value at a time. | Encapsulates data & methods. |
| Visibility | Default is public. |
Default is public. |
Public (Global/Scope based). | Default is private. |
| Use Case | Records (e.g., Student info). | Memory constrained systems. | State machines, flags. | Object Oriented Programming. |
10. Procedural vs. Object-Oriented Programming
Procedural Programming (POP)
- Focus: On functions and logical steps (algorithms).
- Structure: Large programs are divided into functions.
- Data Access: Global data is often moved freely from function to function (less secure).
- Approach: Top-Down approach.
- Example: C, Pascal.
Object-Oriented Programming (OOP)
- Focus: On objects and data.
- Structure: Programs are divided into objects.
- Data Access: Data is hidden (encapsulated) and cannot be accessed by external functions easily.
- Approach: Bottom-Up approach.
- Example: C++, Java, Python.
