You are on page 1of 13

section 1: A2

A)

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. Even
though the prototypes for friend functions appear in the class
definition, friends are not member functions. 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. To declare
a function as a friend of a class, precede the function prototype in the
class definition with keyword friend

Merits of friend function

Friend function act as bridge between two classes by operating on


their private data

Must have access to source code for the class to make a function
into friend.

Able to access members without need of inheriting the class.

Allows a more obvious syntax for calling a function rather than


member function.

Can be declared either in the public or the private part of a class.

Can be used to increase versatility of overloaded operator.

Demerits of friend function

Friend function have access to private members of a class from


outside the class which violates the law of data hiding.
Breach of data integrity

Conceptually messy

Lead to spaghetti-code situation if numerous friends muddy the


boundary between classes.

Maximum size of memory will be occupied by objects according to


size of friend members.

Cannot do any run time polymorphism in its members

B)

Constructors

Constructors are special class functions which performs initialization of


every object. The Compiler calls the Constructor whenever an object is
created. Constructors initialize values to object members after storage is
allocated to the object.

Types of Constructors-Constructors are of three types:

1) Default Constructor- Default constructor is the constructor which


doesn't take any argument. It has no parameter.

2) Parameterized Constructor- These are the constructors with


parameter. Using this Constructor you can provide different values to
data members of different objects, by passing the appropriate values as
argument.

3) Copy Constructor- These are special type of Constructors which takes


an object as argument, and is used to copy values of data members of
one object into other object.
let's take an example to better understand these constructors.

#include<iostream>

using namespace std;

class Cube {

public:

int side;

Cube() { //this is default constructor

side=10;

Cube(int x) { //this is Parameterized constructor

side=x;

Cube (const Cube &c) { //this is copy constructor

side=c.side;

};

int main()

Cube c1; //calls default constructor


Cube c2(15); //calls Parameterized constructor

Cube c3=c2; //calls copy constructor

cout << c1.side<<" "<<c2.side<<" "<<c3.side;

return 0;

output is 10,15,15

section1 :A5

A)

In C++, we have 5 different types of Inheritance. Namely,

1) Single Inheritance

In this type of inheritance one derived class inherits from only one base
class. It is the most simplest form of Inheritance.

2) Multiple Inheritance

In this type of inheritance a single derived class may inherit from two or
more than two base classes.
3) Hierarchical Inheritance

In this type of inheritance, multiple derived classes inherits from a


single base class.

Multilevel Inheritance

In this type of inheritance the derived class inherits from a class, which
in turn inherits from some other class. The Super class for one, is sub
class for the other.

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Mutilevel
Inheritance.

B)

A virtual function is a member function that is declared within a base


class and redefined by a derived class. To create virtual function,
precede the functions declaration in the base class with the keyword
virtual. When a class containing virtual function is inherited, the derived
class redefines the virtual function to suit its own needs.
- Base class pointer can point to derived class object. In this case, using
base class pointer if we call some function which is in both classes, then
base class function is invoked. But if we want to invoke derived class
function using base class pointer, it can be achieved by defining the
function as virtual in base class, this is how virtual functions support
runtime polymorphism.

Without "virtual" you get "early binding". Which implementation of the


method is used gets decided at compile time based on the type of the
pointer that you call through.
With "virtual" you get "late binding". Which implementation of the
method is used gets decided at run time based on the type of the
pointed-to object - what it was originally constructed as.

Example

Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};

Class B: public A
{
int b;
public:
B()
{
b = 2;
}
virtual void show()
{
cout <<b;
}
};

int main()
{
A *pA;
B oB;
pA = &oB;
pAshow();
return 0;
}

Output is 2 since pA points to object of B and show() is virtual in


base class.

Section 1:A7

A structure in the C programming language (and many derivatives) is a


complex data type declaration that defines a physically grouped list of
variables to be placed under one name in a block of memory, allowing
the different variables to be accessed via a single pointer, or the
structure declared name which returns the same address. The structure
can contain many other complex and simple data types in an
association, so is a natural organizing type for records like the mixed
data types.

we use structures when we want a datatype which is a group of


different data types. for ex
If you want to store 1,2,3,4,5,6,7,8,9 , you will use array of int.
If you want to store 'a,'v','r','w','r','q' , you will use array of char.
But, what if you want to store 1,2,'a',4,'b','x' ? what will you use?

program using structures

#include <iostream>

#include <string>

struct Books {

char *title;

char *author;

char *subject;

int book_id;

};

int main( ) {

struct Books Book1; /* Declare Book1 of type Book */

Book1.title= "C Programming";

Book1.author= "Nuha Ali";

Book1.subject= "C Programming Tutorial";

Book1.book_id = 6495407;

/* print Book1 info */

printf( "Book 1 title : %s\n", Book1.title);


printf( "Book 1 author : %s\n", Book1.author);

printf( "Book 1 subject : %s\n", Book1.subject);

printf( "Book 1 book_id : %d\n", Book1.book_id);

return 0;

Section 2: B1

program in C++ to count the number of words in a line of text

#include<iostream>

using namespace std;

int main( )

char str[80];

cout << "Enter a string: ";

cin.getline(str,80);

int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)

if (str[i] == ' ') //Checking for spaces

words++;
}

cout << "The number of words = " << words+1 << endl;

return 0;

section2:B2

/* Example program to show how to transfer values from derived class


constructor to base class constructor*/

#include <iostream>

using namespace std;

class Shape {

public:

Shape(int a,int b) {

cout << "the dimention of shape is "<< a<<"*"<<b<<"\n";

};

class Rectangle: public Shape

protected:

int width;
int height;

public:

Rectangle(int a,int b) : Shape(a,b)

height=a;

width=b;

int getArea() {

return (width * height);

};

int main(void) {

Rectangle *Rect=new Rectangle(5,7);

// Print the area of the object.

cout << "Total area: " << Rect->getArea() << endl;

return 0;

You might also like