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

Dynamic Memory Allocation For Object and Object Array

Uploaded by

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

Dynamic Memory Allocation For Object and Object Array

Uploaded by

nishansubedi15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Dynamic Memory allocation for object and object array

This is the mechanism of allocating memory dynamically for object and object array

Syntax:

Class_name *object_pointer;

Object_pointer=new class_name ; //allocating memory for single object .

Object_pointer=new class_name [size];//allocating memory for an array of object .

Dynamic constructor

1> Constructor can allocate dynamically created memory to object .


2> Thus object is going to use memory region ,which is dynamically created by constructor .

1.WAP with class named BOOK ,which will represent title,author ,price ,publisher.use constructor for
initialization of an objects and use dynamic memory allocation for string type member and destructor in
a proper way .(8)
class book

char *title;

char *author ;

float price;

public:

book(){

Static member variable :

Declared inside the class body and also known as class member variable

They must be defined outside the class

They does not belong to any object but to whole class

There will be only one copy of static member variable for whole class

Any object can use same copy of class variable

They can also be used with class name

Static member function

They are qualified with the keyword static

They are also called class member function

They can be invoked with or without object

They can only access static member of class

//Static Data Member in C++.


//#include <iostream>

//using namespace std;

//

//class Sample{

//public:

// int x; //instance member variable

// static int y; //static member variable or class member variable

//};

//int Sample::y=10;

//int main()

//{

// cout<<"Accessing without object: "<<endl<<"Value of y is: ";

// cout<<Sample::y<<endl;

// Sample s1;

//

// cout<<"\nAccessing with object: "<<endl<<"Value of y is: ";

// cout<<s1.y;

// cout<<endl<<endl;

// return 0;

//}

//Static Member Function in C++.

//#include <iostream>

//using namespace std;

//class Sample{

//private:

// int x;
// static int y;

//public:

// static void display(){

// cout<<y<<endl;

// }

//};

//int Sample::y=10;

//int main()

//{

// cout<<"Accessing static member function without object:"<<endl<<"Value of y is: ";

// Sample::display();

//

// Sample s1;

// cout<<"\nAccessing static member function with object: "<<endl<<"Value of y is: ";

// s1.display();

// return 0;

//Constant Data member in C++

//#include<iostream>

//using namespace std;

//class Circle{

//float r;

//const float pi=3.14; //constant data member....

//public:

// Circle(float a):r(a){}

// void area(){

// cout<<"Area of circle: "<<pi*r*r<<endl;


// }

//};

//int main(){

//Circle c1(2);

//c1.area();

//return 0;

//}

////Constant member function in C++....

//#include <iostream>

//using namespace std;

//

//class Laptop{

// string Lname;

//public:

// Laptop(string name):Lname(name){}

// void display() const{ //constant member function in C++

// cout<<"Brand Name of Laptop: "<<Lname<<endl;

// }

//};

//int main(){

// Laptop L1("Dell"),L2("Acer");

// L1.display();

// L2.display();

// return 0;

//
Function overloading

Void add(int,int)

Void add(int,int)

Void add(int,int,int)

Void add (int ,float)

Default argument

Void add(int a=2 ,int b ,char c=’a’)

Inline function

#include <iostream>

// Define an inline function to compute the square of a number

inline int square(int x) {

return x * x;

int main() {

int a = 5;

int b = 10;

// Call the inline function

std::cout << "The square of " << a << " is " << square(a) << std::endl;

std::cout << "The square of " << b << " is " << square(b) << std::endl;

return 0;}
friend class and friend function

2. Friend Function and Friend Class:

2.1. Friend Function:

 It is not a member function of a class to which it is friend.

 It is declared in the class with friend keyword.

 It must be defined outside the class to which it is friend.

 It can access any member of the class to which it is friend.

 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.

 It can not access members of the class directly.

 It has no caller object.

 It should not be defined with membership label.

 Friend function can become friend to more than one class.( it is unique behavior of friend

function ).

Sample Program:

#include <iostream>

using namespace std;

class Sample{

int x;

public:

Sample(){

x=0;

}
void display(){

cout<<"Value of x is: "<<x<<endl;

friend void add(Sample &s); //friend function's prototype. //pass by reference is opt.

};

void add(Sample &s){ //friend function definition.

s.x=s.x+4;

int main()

Sample s1;

s1.display();

add(s1); //friend function call.

s1.display();

return 0;

Your Work:

1. WAP to find sum of two distance (feet, inch) objects using friend function.

2.2. Friend Class

2.2. Friend Class:

 A friend class can access private and protected members of other class in which it is declared

as friend. It is sometimes useful to allow a particular class to access private members of other

class.

 Following are some important points about friend functions and classes:

1) Friends should be used only for limited purpose.

2) Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A

automatically.

3) Friendship is not inherited.

4) The concept of friends is not there in Java.


Sample Program:

#include <iostream>

using namespace std

class A {

int x;

public:

A(){

x=0;

friend class B;

};

class B{

public:

void display(A &a){ //pass by reference is opt.

cout<<"Value of x: "<<a.x<<endl;

};

int main() {

A a1;

B b1;

b1.display(a1);

return 0;

Your Work:

1. Create a class mdistance to store the values in meter and centimeter and class edistance to store

values in feet and inches. Perform addition of object of mdistance and object of edistance by using

friend function

You might also like