Вы находитесь на странице: 1из 8

1) Distinguish between pointer and reference using an example.

Ans}

Pointer: A pointer is a variable that holds memory address of another variable.


A pointer needs to be dereferenced with * operator to access the memory
location it points to.

Reference: A reference variable is an alias, that is, another name for


an already existing variable. A reference, like a pointer is also
implemented by storing the address of an object
You can't take address of a reference like you can with pointers.

A pointer can be re-assigned while reference cannot, and must be assigned at


initialization only.

Pointer can be assigned NULL directly, whereas reference cannot.

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;

int *ptr = &b;

Reference:

int b=10;

int &p=a;
2} How would you implement a destructor? How is it useful?

Ans}

Destructor is a member function which destructs or deletes an object.


Destructors have same name as the class preceded by a tilde (~). Destructors
don’t take any argument and don’t return anything

For eg.:

#include<iostream.h>

using namespace std;

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

3} Create a class called Student having 3 private variables.


(Roll number, Marks, Student ID). Create member functions to
access the variables and update them.
Ans}
#include<iostream.h>

using namespace std;

class student

private:

int rollno,marks,stuid;

public:

student()

rollno=0;

marks=0;
stuid=0;

void get()

cout<<”Enter Roll number of Student”<<endl;

cin>>rollno;

cout<<”Enter Marks Of student”<<endl;

cin>>marks;

cout<<”Enter Student ID of Student”<<endl;

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;

4} WAP to show pointer to class, pointer to class member and pointer


to function. Explain

Ans}

#include<iostream>

using namespace std;

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;

star* ptr; //pointer of class

ptr=&obj;

cout<<obj.a<<endl;

cout<<ptr->a<<endl;

int star::*ptr1=&star::a; // pointer to data member 'a'

cout<<obj.*ptr1<<endl;

ptr2=&star::print;

return 0;

5. What is a friend function? WAP using friend function. Explain how is it


useful?

Ans}

A friend function of a class is defined outside that class'


scope but it has the right to access all private and
protected members of the class.

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.

A friend can be a function, function template, or member function, or a


class or class template, in which case the entire class and all of its
members are friends.

Program:

#include <iostream>
using namespace std;

class star

private:

int meter;

public:

star (): meter(0) { }

friend int addFive(star);

};

int addFive(star d)

d.meter += 5;

return d.meter;

int main()

{
star D;

cout<<" star: "<< addFive(D);

return 0;

Вам также может понравиться