Assign 2 Oop
Assign 2 Oop
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.
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 :
class scaler{
static int number; // static data member is declared
};
Syntax:
Example:
class scaler{
static int number;
static void get_no_of_topics() //static function declaration
{
cout<<number<<"\n";
};
int scaler::number;
#include<iostream>
class A
public:
A()
{
count++;
} };
int A::count;
int main()
{ A obj1,obj2,obj3,obj4;
cout<<A::count;
Q 4: Write a program for finding the GCD of two positive integers using
reference types as function parameters.
#include <iostream>
if (a % i == 0 && b % i == 0) {
gcd = i;
break;
int main() {
int a, b;
int gcd = 1;
findGCD(a, b, gcd);
std::cout << "The GCD of " << a << " and " << b << " is "
<< gcd << std::endl;
return 0;
E.G :
#include <iostream>
Class X {
int a, b ;
int add();
};
// global variable
int a = 10;
int main() {
int answer;
X xobject;
xobject.a = 1;
xobject.b = 2;
answer = xobject.add();
cout << xobject.a << " + " << xobject.b << " = " << answer << endl;
#include <iostream>
class Base {
private:
public:
Base() {
created++;
~Base() {
destroyed--;
return created;
return destroyed;
};
int Base::created = 0;
int Base::destroyed = 0;
int main() {
Base arr[10];
Base another_one;
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:
void printDate() {
std::cout << month << "/" << day << "/" << year << std::endl;
month = m;
day = d;
year = y;
};
int main() {
Date date1;
date1.printDate();
date2.printDate();
date1.printDate();
return 0;