0% found this document useful (0 votes)
30 views6 pages

Assignment 4 Co

The document discusses key concepts in procedural programming (POP) and object-oriented programming (OOP). It explains that POP focuses on separating data and functions, while OOP combines data and functions within objects. It also notes that OOP emphasizes concepts like inheritance and polymorphism that allow for code reuse and flexibility.

Uploaded by

ateebtahir527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Assignment 4 Co

The document discusses key concepts in procedural programming (POP) and object-oriented programming (OOP). It explains that POP focuses on separating data and functions, while OOP combines data and functions within objects. It also notes that OOP emphasizes concepts like inheritance and polymorphism that allow for code reuse and flexibility.

Uploaded by

ateebtahir527
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ans 1) a) A union is a data structure in programming that allows you to store different data types in

the same memory loca on. For eg:-consider a union named Data with integer, float, and character
members. When you assign a value to one member, the others may contain garbage data, as they
share the same memory.

b)The key difference between a union and a structure is that structure allows us to store mul ple
members with their own memory space. Each member in a struct can hold a value simultaneously. In
contrast, a union shares memory among its members, so only one member can have a value at any
given me.

c) # Advantages of Union:

 Memory-efficient: Unions save memory by sharing memory space among members.


 Useful in specific scenarios where you need to represent different data types in a compact
manner.
# Disadvantages of Union:
 Limited usability: Unions are not suitable for scenarios where you need to store mul ple
values simultaneously, unlike structures.
 Type safety: Unions can be error-prone as there's no built-in way to track which member is
currently ac ve, leading to poten al bugs if used improperly.

d)i) The difference between macros and func ons are as follows:

Invoca on:

Macros: Macros are expanded by the preprocessor at compile- me. This means that the code they
represent is inserted directly into your program before it's compiled.

Func ons: Func ons are executed at run me. They are called and perform their tasks when your
program is running.

Type Safety:

Macros: Macros don't provide type checking. They are essen ally text subs tu ons and don't
enforce type safety, which can lead to type-related errors if not used carefully.

Func ons: Func ons are type-safe. They have well-defined return types, and the compiler enforces
type checking, ensuring that the return value matches the specified type.

Debugging:

Macros: Debugging macros can be challenging. You can't set breakpoints inside macros, and their
behaviour is hidden within the preprocessor stage, making it harder to trace their execu on.

Func ons: Func ons are easier to debug. You can set breakpoints, inspect their behavior, and use
standard debugging tools to understand their execu on, making it more straigh orward to iden fy
and resolve issues.

Macro code:

#include <stdio.h>

#define display(i) prin ("X" #i "lmao %d",x##i) //macro

int main(){
int x1=33;

display(1);

return 0;

Func on code:

int fact(int n){

if(n==0 || n==1){

return 1;

else{

return n*fact(n-1);

ii)Limita ons of macros are as follows:

 No Type Checking: Macros lack type checking, making it possible to use them with
incompa ble data types, poten ally leading to run me errors.
 Lack of Encapsula on: Macros are not encapsulated like func ons, which can result in
naming conflicts and unintended side effects.
 Readability and Debugging: Complex macros can reduce code readability, and debugging
them is challenging since they are expanded at the preprocessing stage, making it hard to
inspect their behaviour.

Ans 2) CODE

#include <stdio.h>

struct Date {

int day;

int month;

int year;

};

int main() {

struct Date date1, date2;

prin ("Enter the first date (day month year): ");

scanf("%d %d %d", &[Link], &[Link], &[Link]);


prin ("Enter the second date (day month year): ");

scanf("%d %d %d", &[Link], &[Link], &[Link]);

if ([Link] == [Link] && [Link] == [Link] && [Link] == [Link]) {

prin ("Dates are same\n");

} else {

prin ("Dates are different\n");

Ans 3) CODE

#include <stdio.h>

int productOfEight(int n) {

return n * 8;

int divideByTwo(int n) {

return n / 2;

int main() {

int number;

prin ("Enter a number: ");

scanf("%d", &number);

int productResult = productOfEight(number);

int divideResult = divideByTwo(number);

prin ("Product of %d and 8 is: %d\n", number, productResult);

prin ("Value obtained when %d is divided by 2 is: %d\n", number, divideResult);

return 0;

Ans 4) i) Handling files involves several key steps:

1. Include Header Files: Include necessary header files like <stdio.h> for standard I/O func ons
and <stdio.h> for memory-related func ons.

2. Declare File Pointer: Declare a file pointer, typically of type FILE*, to reference the file during
opera ons.
3. Open, Operate, and Close: Use fopen to open the file, perform read or write opera ons
using appropriate func ons, and close the file with fclose to prevent data loss or corrup on.
Proper error handling and cleanup are essen al throughout the process.

ii) File modes specify how files can be accessed and modified. For example:

1. "r" (Read Mode): Opens a file for reading. If the file doesn't exist, it returns a null pointer.
Requires the <stdio.h> header.

2. "w" (Write Mode): Opens a file for wri ng. Exis ng content is truncated; if the file doesn't
exist, it's created. Requires the <stdio.h> header.

3. "a" (Append Mode): Opens a file for wri ng at the end. If the file doesn't exist, it's created.
Requires the <stdio.h> header.

Ans 5) 1. Approach:

 POP uses a step-by-step, func on-centric approach.

 OOP uses a real-world object-centric approach.

2. Data Handling:

 POP separates data and func ons.

 OOP combines data and func ons within objects.

3. Organiza on:

 POP organizes code around procedures and func ons.

 OOP organizes code around objects and their interac ons.

4. Inheritance and Polymorphism:

 POP doesn't emphasize these concepts.

 OOP relies on inheritance and polymorphism for code structure and reusability.

Ans 6) a) Classes: In OOP, classes serve as blueprints, defining the structure and behaviour of objects.
They encapsulate a ributes and methods for crea ng instances.

(b) Objects: Objects are instances of classes, represen ng real-world en es with their own data and
behaviour.

(c) Data Abstrac on and Encapsula on: Abstrac on hides complex details, while encapsula on
bundles data and methods, enhancing security and maintainability.

(d) Inheritance: Inheritance enables new classes to inherit proper es and methods from exis ng
ones, promo ng code reusability.

(f) Polymorphism: Polymorphism allows different objects to respond to the same method differently,
enhancing code flexibility and extensibility.

(g) Dynamic Binding: Dynamic binding selects methods at run me based on object type, a key
feature of polymorphism.
(h) Message Passing: Objects communicate by sending messages, invoking methods to interact and
perform ac ons, facilita ng object interac ons in OOP.

Ans 6)

a) C!it!for!yourself

b) Will give error because s2 is defined as a constant so it cannot allow change.

c) a=0 b=-1 c=0

d) c=0 d=1

e) Square of 1.00 is 1.00

Square of 1.10 is 1.21

Square of 1.20 is 1.44

Square of 1.30 is 1.69

f) j=4

You might also like