Lecture 7 - Structs and Data Abstraction Flashcards
What are compound data types in C?
Compound data types allow multiple variables to be grouped under a single identifier. The members of these types need not be of the same type and are addressed by name. They are implemented via struct
in C.
What is a struct
in C?
A struct
in C is a user-defined data type that groups together variables under one name, where each variable can be of a different type. Example: struct Fraction { int numerator; int denominator; };
.
How do you declare and access a struct
variable?
You declare a struct
variable by defining the struct
type and then creating an instance. Accessing members is done using the dot operator (.) or the arrow operator (->) when dealing with pointers. Example: f.numerator = 3; pf->denominator = 4;
.
What is the typedef
statement in C?
The typedef
statement allows you to create an alternative name for a data type. It simplifies the declaration of struct
variables. Example: typedef struct { int numerator; int denominator; } Fraction;
allows you to use Fraction
instead of struct Fraction
.
How can you multiply two fractions using a struct
in C?
You can multiply two fractions by accessing and multiplying the numerator and denominator of each fraction. Example: h.numerator = f.numerator * g.numerator; h.denominator = f.denominator * g.denominator;
.
What is procedural abstraction in C?
Procedural abstraction is a programming principle where complex procedures or functions are broken down into simpler ones. It involves defining clear interfaces and implementation details, allowing a program to be understood at different levels of abstraction.
What are the key components to define in a function for procedural abstraction?
You need to define the interface to the function, the conditions under which it will operate correctly, what the function does, any side effects, and the exceptions/errors it might raise.
What is the purpose of initFraction
in C?
The initFraction
function initializes the members of a Fraction
struct with specific values. Example: void initFraction(Fraction *pf, int numerator, int denominator)
assigns the numerator and denominator values.
How can you make a program modular using procedural abstraction?
You can break the program into smaller functions that perform specific tasks, hiding implementation details within each module and focusing on the interface. This modular approach allows easier understanding and maintenance of large programs.
What is the advantage of using abstract data types (ADTs) in C?
Abstract data types (ADTs) hide the implementation details of a data structure, exposing only a set of functions for manipulating the data. This makes the program more modular, reduces complexity, and allows changes in implementation without affecting the code that uses the ADT.
What does it mean to encapsulate data within an abstract data type (ADT)?
Encapsulation in an ADT means that the data is not directly accessible. Instead, a set of interface functions provides controlled access to the data, ensuring consistency and allowing error checking (e.g., ensuring a denominator is never zero in a fraction).
How can you change the internal representation of a struct
without affecting the client code?
By using abstract data types (ADTs), the internal representation of a struct
can be modified, as long as the interface functions remain consistent. Client code will continue to work without changes since it interacts only with the interface, not the implementation.
What is the role of the printFraction
function in C?
The printFraction
function displays the numerator and denominator of a Fraction
struct in the format numerator/denominator
using formatted output.
What is an example of error checking in an abstract data type?
An example of error checking in an ADT is ensuring that the denominator of a fraction is never zero. This can be implemented in the interface functions, which manage access to the data.
What is a key benefit of modular and weakly coupled programs?
Modular and weakly coupled programs are easier to maintain and debug because each module or component performs a specific task and can be changed or replaced without affecting the rest of the program.