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

Assign 2 Oop

The document contains the assignment questions for an Object Oriented Programming class. It includes 7 questions related to function prototypes, static class members, constructors, reference parameters, scope resolution operator, and defining a Date class. The questions cover fundamental OOP concepts like classes, objects, methods, constructors, static members, and operator overloading. Sample code is provided for each question to demonstrate the concept being asked about.

Uploaded by

aleena.faizyab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assign 2 Oop

The document contains the assignment questions for an Object Oriented Programming class. It includes 7 questions related to function prototypes, static class members, constructors, reference parameters, scope resolution operator, and defining a Date class. The questions cover fundamental OOP concepts like classes, objects, methods, constructors, static members, and operator overloading. Sample code is provided for each question to demonstrate the concept being asked about.

Uploaded by

aleena.faizyab
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT #2

Object Oriented Programming


CLO1-PLO1

NAME : ALEENA FAIZYAB


REG# : FA21-BSI-003
SUBMITTED TO: MAM SADAF
DATE : 17TH APRIL 2023

Q 1: Define function prototype. Describe different styles of writing function


prototypes. Find the errors if any in the following function prototypes:
a) Float average(x,y);
b) Int mul(int a, b);
c) Void print(float data[],size=20);

Function Prototype:
A function prototype is a declaration of the function that tells the program about the type of
value returned by the function and the number and type of arguments. A function prototype
describes the function interface to the compiler by giving details such as the number and type of
arguments and the type of return values. The prototype declaration looks just like a function
definition, except that it has no body and its code is missing. This is the difference between a
declaration and a definition.
A declaration introduces a function name to the program, whereas a definition is a declaration
that also tells the program what the function is doing and how it is doing it.

A function prototype has the following parts:

 return type
 name of the function
 argument list.

E.G : char function_name( int lower, int *upper, char (*func)(), double y );

Prototype style:
 Functions are declared explicitly with a prototype before they are called. Multiple
declarations must be compatible; parameter types must agree exactly.
 Arguments to functions are converted to the declared types of the parameters.
 The number and type of arguments are checked against the prototype and must agree with
or be convertible to the declared types. Empty parameter lists are designated using
the void keyword.
 Ellipses are used in the parameter list of a prototype to indicate that a variable number of
parameters are expected.

Errors in :

a) Float average(x,y); No data type for each variable in argument list.


b) Int mul(int a, b); No data type for variable b in argument list.
c) Void print(float data[],size=20);

Q 2: Define static data member and static member functions. When do we


declare a member of class static?

Static Data Members:


Static data members in C++ are used to store values common to the entire class. A static data
member is available globally for all the objects of the same class. Unlike the regular (non-static)
data members of a class- A static data member maintains its single copy that is shared by all the
objects of the class. It remains in the memory for the entire program but is visible only within the
class or the function within which it is defined. To make a data member static, Declaration of the data
member should be within the class definition and Its definition should be outside the class definition.
Example:

class scaler{
static int number; // static data member is declared
};

int scaler::number; // static data member is defined

Static Member Functions:


Static member functions are the functions that can access only the static data members. These
static data members share a single copy of themselves with the different objects of the same
class. A function can be made static by using the keyword static before the function name while
defining a class.

Syntax:

static returntype function_name(){}

Example:

class scaler{
static int number;
static void get_no_of_topics() //static function declaration
{
cout<<number<<"\n";
};
int scaler::number;

Q 3: Write a program to count the number of objects in a class.

#include<iostream>

using namespace std;

class A

public: static int count;

public:

A()

{
count++;

} };

int A::count;

int main()

{ A obj1,obj2,obj3,obj4;

cout<<"no of objects created= ";

cout<<A::count;

Q 4: Write a program for finding the GCD of two positive integers using
reference types as function parameters.

#include <iostream>

void findGCD(int a, int b, int& gcd) {

int min = (a < b) ? a : b;

for (int i = min; i >= 1; i--) {

if (a % i == 0 && b % i == 0) {

gcd = i;

break;

int main() {
int a, b;

std::cout << "Enter two positive integers: ";

std::cin >> a >> b;

int gcd = 1;

findGCD(a, b, gcd);

std::cout << "The GCD of " << a << " and " << b << " is "
<< gcd << std::endl;

return 0;

Q 5: Explain the use of the scope resolution operator with an example.

Scope Resolution Operator


It is used to define a function outside the class and used to access the static variables of
class. Whenever the definition of a class member appears outside of the class declaration,
the member name must be qualified by the class name using the :: (scope
resolution) operator.

E.G :

#include <iostream>

using namespace std;

Class X {
int a, b ;

// member function declaration only

int add();

};

// global variable

int a = 10;

// define member function outside its class declaration

int X::add() { return a + b; }

int main() {

int answer;

X xobject;

xobject.a = 1;

xobject.b = 2;

answer = xobject.add();

cout << xobject.a << " + " << xobject.b << " = " << answer << endl;

Q 6: Write a C++ program to count the number of objects created and


destroyed for a class using static data members and static member functions
(hint: increment when obj is created and decrement when obj is destroyed)

#include <iostream>

class Base {
private:

static int created;

static int destroyed;

public:

Base() {

created++;

~Base() {

destroyed--;

static int CtorCalls() {

return created;

static int DtorCalls() {

return destroyed;

};

int Base::created = 0;
int Base::destroyed = 0;

int main() {

Base arr[10];

Base another_one;

std::cout << "Ctors calls: " << Base::CtorCalls() << "\n";

std::cout << "Dtors calls: " << Base::CtorCalls() << "\n";

return 0;

Q : 7 Write a definition of a class named Date that contains three elements the
month, the day of the month, and the year, all of the type int.
 Write two constructors, a default constructor (that initializes each data
element of an object with zero) and a constructor that takes three parameters
(the month, the day of the month, and the year) and initializes the data
member of the object with these parameters.
 Write a function void printDate() that displays the data elements of the
object.

 Write a function void setDate(int, int, int) that takes three parameters (the
month, the day of the month, and the year) and initialize the data member of
the object with these parameters.
Write a main function to create two objects of class Date, the data member of
one object is initialized with zero through the default constructor. The data
member of the second object is initialized with some values using a
constructor that takes three parameters.

#include <iostream>

class Date {

private:

int month;

int day;

int year;

public:

Date() : month(0), day(0), year(0) {}

Date(int m, int d, int y) : month(m), day(d), year(y) {}

void printDate() {

std::cout << month << "/" << day << "/" << year << std::endl;

void setDate(int m, int d, int y) {

month = m;

day = d;

year = y;

};
int main() {

Date date1;

Date date2(4, 16, 2023);

std::cout << "Date 1: ";

date1.printDate();

std::cout << "Date 2: ";

date2.printDate();

date1.setDate(12, 31, 2023);

std::cout << "Updated Date 1: ";

date1.printDate();

return 0;

You might also like