Structures In C++
Topic asked in February 2022 (CBCS) and July 2022 (CBCS) question papers.
In C++, a structure is a user-defined data type that allows you to group together different variables under a single name.
You can define a structure using the struct
keyword, declare structure variables, and access members of the structure.
Let's go through each of these steps with examples:
Defining a Structure
To define a structure in C++, you use the struct
keyword followed by the name of the structure and a set of curly braces enclosing the member variables.
Example:
Declaring Structure Variables
After defining a structure, you can declare variables of that structure type. This is similar to declaring variables of any other data type.
Example:
Accessing Members of Structure
You can access the members (variables) of a structure using the dot (.) operator followed by the member name.
Example:
Putting it all together, here's a complete example demonstrating the definition, declaration, and access of structure members:
Output:
Class In C++
In C++, a class is a user-defined data type that provides a template for creating objects (instances) which encapsulate data and behavior. A class is a blueprint that defines the structure and behavior of an object. It defines the data members and member functions that objects of that class will have. An object is an instance of a class. It is a concrete entity that has its own set of data members and member functions.
Defining a Class
To define a class in C++, you use the class
keyword followed by the name of the class and a set of curly braces enclosing the member variables and member functions.
Example:
Declaring Objects of Class
After defining a class, you can declare objects (instances) of that class. This is similar to declaring variables of any other data type.
Example:
Accessing Class Members
You can access the members (variables and functions) of a class using the dot (.) operator followed by the member name.
Putting it all together, here's a complete example demonstrating the definition of a class, declaration of objects, and access of class members:
Output:
Difference Between struct and class in C++
Topic asked in May 2024 (NEP) question paper.
Struct | Class |
---|---|
The data member of the struct is public by default. | The data member of the class is private by default. |
The structure is defined by using the struct keyword. | The class keyword is used to define the class in C++. |
The structure cannot be inherited. | The class can be inherited. |
The structure does not provide security. | The class provides high security. |
It is mostly used for a small amount of data. | It is used for a large amount of data. |
Difference between Array and Structure In C++
Topic asked in July 2022 (CBCS) and December 2022 (CBCS) question paper.
Feature | Array | Structure |
---|---|---|
Data Types | All elements of an array have the same data type. | Each member of a structure can have a different data type. |
Memory Allocation | Contiguous memory allocation for elements of the same data type. | Non-contiguous memory allocation for members of different data types. |
Size | Fixed size, determined at the time of declaration. | Variable size, determined by the sum of sizes of its members. |
Access | Elements are accessed using an index. | Members are accessed using the dot (.) operator. |
Homogeneity | Homogeneous data type (all elements are of the same type). | Heterogeneous data type (members can be of different types). |
Declaring an Array of Structures
We can declare an array of structures by combining the concepts of arrays and structures. Here's how we can do it:
Output:
Explanation:
In this example, we define a structure Student with three members: name, rollNumber, and marks. Then, we declare an array students of type Student with a fixed size of 3. We initialize the array elements with the details of different students. Finally, we access and print the details of each student using a loop.
Passing Structure Through A Function
Topic asked in July 2022 (CBCS) question paper.
In C++, structures can be passed to functions in several ways:
-
Pass by Value: When a structure is passed by value, a copy of the entire structure is made and passed to the function. Any modifications made to the structure inside the function do not affect the original structure outside the function.
Output:
Explanation:
- Inside the printPoint function, the x coordinate of the passed structure p is modified to 10.
- After returning from the function, the x coordinate of the original myPoint structure remains unchanged (3), demonstrating that modifications inside the function do not affect the original structure.
-
Pass by Reference (or Address): When a structure is passed by reference, a reference (or pointer) to the original structure is passed to the function. Any modifications made to the structure inside the function affect the original structure outside the function.
Output:
Explanation:
- Inside the printPoint function, the x coordinate of the passed structure p is modified to 10.
- After returning from the function, the x coordinate of the original myPoint structure is also 10, demonstrating that modifications inside the function affect the original structure since it was passed by reference.
OOP
Topic asked in July 2022 (CBCS) question paper.
OOP stands for Object-Oriented Programming. It's a programming paradigm that revolves around the concept of "objects," which can contain data in the form of fields (attributes or properties) and code in the form of procedures (methods or functions).
The key features of OOP in C++ include:
- Classes and Objects: Classes are user-defined data types that define properties and behaviors. Objects are instances of classes.
- Encapsulation: Encapsulation in C++ is a fundamental concept of Object-Oriented Programming (OOP) that involves bundling the data (variables) and methods (functions) that operate on the data into a single unit, called a class.
- Abstraction: Abstraction in C++ is the process of hiding the complex implementation details of a class or an object, and only exposing the essential features or behaviors that are relevant to the user. It allows the user to focus on what an object does rather than how it does it.
- Inheritance: Inheritance is a mechanism where a new class inherits properties and behaviors from an existing class. It promotes code reusability and enables the creation of a hierarchical class structure.
- Polymorphism: Polymorphism allows objects of different classes to respond differently to the same method call.
Last updated on -