Professional Documents
Culture Documents
Ans}
Pointers can iterate over an array, we can use ++ to go to the next item that a
pointer is pointing to.
A pointer is a variable that holds a memory address. A reference has the same
memory address as the item it references.
For Example:
Pointers:
int b = 20;
Reference:
int b=10;
int &p=a;
2} How would you implement a destructor? How is it useful?
Ans}
For eg.:
#include<iostream.h>
class star
private:
int x=10,y=20;
public:
~star()
cout<<”\nDestructor is called”;
void print()
cout<<”x=”<<x<<”y=”<<y<<endl;
};
Int main ()
star a;
return 0;
OUTPUT :-
x=10 y=20
class student
private:
int rollno,marks,stuid;
public:
student()
rollno=0;
marks=0;
stuid=0;
void get()
cin>>rollno;
cin>>marks;
cin>>stuid;
void print()
cout<<”Roll Number=”<<rollno<<endl;
cout<<”Marks=”<<marks<<endl;
cout<<”Student ID:-“<<stuid<<endl;
};
int main()
student A;
A.get();
A.print();
return 0;
Ans}
#include<iostream>
class star
public:
int a=10;
void print(int b)
a=b;
cout<<a<<endl;
};
void (star::*ptr2)(int)=&star::print;
int main()
{
star obj;
ptr=&obj;
cout<<obj.a<<endl;
cout<<ptr->a<<endl;
cout<<obj.*ptr1<<endl;
ptr2=&star::print;
return 0;
Ans}
Friends should be used only for limited purpose. too many functions or
external classes are declared as friends of a class with protected or private
data, it lessens the value of encapsulation of separate classes in object-
oriented programming.
Program:
#include <iostream>
using namespace std;
class star
private:
int meter;
public:
};
int addFive(star d)
d.meter += 5;
return d.meter;
int main()
{
star D;
return 0;