0% found this document useful (0 votes)
26 views

Lab 12

Lab 12 oop

Uploaded by

hunain akhtar
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)
26 views

Lab 12

Lab 12 oop

Uploaded by

hunain akhtar
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
You are on page 1/ 9

Name Hunain Akhtar

CLASS EEE-3

Teacher Nayab Gogosh

REG NO FA23-EEE-017

ASSIGNMENT-1
1. Answer these
A. Explain with suitable example, syntax of for loop in C++. And discuss
how it is di erent from C syntax.
B. Explain concept of abstract class.
C. State the use of scope resolution operator and its use in C++.
D. State use of new operator.
E. List and explain use of any four file mode parameters
F. Explain use of friend function with the help of suitable example.
G. Write a C++ program to swap two numbers using pointer

ANSWER
A. For Loop in C++:

Syntax:

for (initialization; condition; update) {

Statements to be executed}

Example:

for (int i = 0; i < 5; i++) {

cout << "Iteration " << i << endl;

Di erences from C:

* Initialization: C allows multiple variable declarations, while C++ restricts it to one.

* Empty Body: C requires a semicolon after an empty loop body, while C++ doesn't.

* Range-Based for Loops: C++ has range-based for loops for iterating over containers,
which C lacks.
B) . Explain concept of abstract class

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for
other classes, defining common methods and attributes that derived classes must
implement. Abstract classes contain at least one pure virtual function, a function declared
with the virtual keyword and assigned the value 0. Derived classes must provide concrete
implementations for all inherited pure virtual functions

EXAMPLE

class Shape {

public:

virtual double area() = 0;

};

class Circle : public Shape {

public:

double area() override {

};

C. State the use of scope resolution operator and its use in C++.
ANSWER

The scope resolution operator (:) is used to access members of a class that are hidden by
other declarations within the same scope. It also allows you to access static members of a
class without creating an instance of the class.

D. State use of new operator.


The new operator is used to dynamically allocate memory for objects on the heap. It
returns a pointer to the newly allocated memory. You are responsible for deallocating this
memory using the delete operator when it's no longer needed to avoid memory leaks.

Example:
int* ptr = new int;

*ptr = 10;

cout << *ptr << endl;

delete ptr;

E. List and explain use of any four file mode parameters


1. ios::in: Opens a file for input.
2. ios::out: Opens a file for output (creating it if it doesn't exist or overwriting
its contents).
3. ios::app: Opens a file for output, appending data to the end if it exists. 4.
ios::binary: Opens a file in binary mode, preserving byte-level data.
Example:
ifstream inputFile("data.txt", ios::in);
ofstream outputFile("output.txt", ios::out | ios::binary);

F. Explain use of friend function with the help of suitable example


A friend function is a function that has access to the private members of a
class, even if it's not defined within the class. It's declared as a friend using
the friend keyword within the class definition. Friend functions are often used
for non-member functions that need to operate on private data of a class.
Example:
class MyClass {
private:
int data;
public:
friend void printData(MyClass obj);
};
void printData(MyClass obj) {
cout << obj.data << endl;
}

G. Write a C++ program to swap two numbers using pointer


#include <iostream>
using namespace std;
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 10, b = 20;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
swap(&a, &b);
cout << "After swapping: a = " << a << ", b = " << b << endl;
return 0;
}
2.Make a class Home , which has three data member length, breadth and
height. The member functions are constructor, destructor , calculate
area, calculate volume.
Program:

3.Make a program for the given data

class Human

{ private:

EyeColor IColor;

NAME personname;

public:

void SetName(NAME anyName);

void SetIColor(EyeColor eyecolor); };


Program:

Output:
4.

class person

char name[20];

int id;

public:

void getdetails(){}

};

Program:
5. Write a C++ program to display number of objects created using static member.

You might also like