OOP Module 15
OOP Module 15
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
ptr = var;
for (int i = 0; i < MAX; i++) {
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
ptr++;
}
return 0;
}
OUTPUT:
b) To implement a program to return name and age of elder person using this pointer.
CODE:
#include<iostream>
#include<string.h>
using namespace std;
class person{
public:
string name;
int age;
void getDetails(){
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
void print(){
cout << "Elder Person is ";
cout << name;
cout << ". His age is ";
cout << age;
}
person display(person x){
if(this->age > x.age){
return *this;
}
else{
return x;
}
}
};
int main(){
person p1, p2, p3;
p1.getDetails();
p2.getDetails();
p3 = p1.display(p2);
p3.print();
return 0;
}
OUTPUT:
c) To implement a program to find sum of squares and sum of cubes by passing functions as
arguments to function sum using pointers.
CODE:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Calculate{
public:
int sum (int *ptr){
int sumation = 0;
for(int i =0; i<6; i++){
sumation += ptr[i];
}
return sumation ;
}
int *square(int *num){
for(int x = 0; x<6; x++){
num[x] *= num[x];
}
cout << endl;
return num;
}
int *cube(int *num){
for(int x = 0; x<6; x++){
num[x] = num[x] * num[x] * num[x];
}
return num;
}
};
int main(){
int num[6];
cout << "Enter 6 numbers: ";
for(int i= 0; i<6; i++){
cin >> num[i];
cout << "num["<<i<<"] = "<<num[i]<<endl;
}
int otherNum[6];
for(int i= 0; i<6; i++){
otherNum[i] = num[i];
}
Calculate c;
cout << "Sum of Squares = " << c.sum(c.square(num)) << endl;
cout << "Sum of Cubes = " << c.sum( c.cube(otherNum)) << endl;
return 0;
}
OUTPUT:
d) To implement a program to accept & display matrix using dynamic memory allocation.
CODE:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int *rollno;
float *marks;
int main(){
int size, i;
cout<<"How many elements for the array ? ";
cin>>size;
rollno = new int[size];
marks = new float[size];
if((!rollno) || (!marks)){
cout << "Out of Memory..!!..Aborting..!!\n";
cout << "Press any key to exit..";
getch();
exit(1);
}
for(i=0; i<size; i++){
cout << "Enter rollno and marks for student " << (i+1) << "\n";
cin >> rollno[i] >> marks[i];
}
cout<<"\nRollNo\t\tMarks\n";
for(i=0; i<size; i++){
cout << rollno[i] << "\t\t" << marks[i] << "\n";
}
delete[]rollno;
delete[]marks;
} OUTPUT:
e) To implement a program to demonstrate pointers.
1. Void pointer
CODE:
#include<iostream>
using namespace std;
int main(){
int x = 10;
float y = 15.5;
void *ptr;
ptr = &x;
cout << "Integer Variable: " << *((int *)ptr) << endl;
ptr = &y;
cout << "Float Variable: " << *((float *)ptr) << endl;
return 0;
}
OUTPUT:
2. NULL Pointer.
CODE:
#include<iostream>
using namespace std;
int main(){
int *ptr;
ptr = NULL;
cout << "The value of NULL pointer is: " << ptr;
return 0;
}
OUTPUT:
3. Dangling Pointer
CODE:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
int *ptr = (int*)malloc(sizeof(int));
*ptr = 10;
cout << "The value of Dangling Pointer is:" << *ptr;
free(ptr);
ptr = NULL;
return 0;
}
OUTPUT: