Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

PROGRAMMING IN C++ - CSE2036

LAB SHEET – 3
1. Array of Objects – Declare and Initialize Separately
#include <iostream>
using namespace std;

class Student {
public:
string name;
int rollno;

Student() {}

Student(string x, int y) {
name = x;
rollno = y;
}

void printDetails() {
cout << rollno << " - " << name << endl;
}
};

int main() {
//declare array with specific size
Student students[5];

//assign objects
students[0] = Student("Ram", 5);
students[1] = Student("Alex", 1);
students[2] = Student("Lesha", 4);
students[3] = Student("Emily", 3);
students[4] = Student("Anita", 2);

for(int i=0; i < 5; i++) {


students[i].printDetails();
}
}

2. Array of Objects – Declare and Initialize in a Single Line


#include <iostream>
using namespace std;

class Student {
public:
string name;
int rollno;

Student() {}

Student(string x, int y) {
name = x;
rollno = y;
}

void printDetails() {
cout << rollno << " - " << name << endl;
}
};

int main() {
//declare array with specific size
Student students[] = {Student("Ram", 5), Student("Lesha", 4),
Student("Anita", 2)};

for(int i=0; i < 3; i++) {


students[i].printDetails();
}
}

3. Initialize Array of objects with parameterized constructors in C++


#include <iostream>
using namespace std;

class Test {
// private variables.
private:
int x, y;

public:
// parameterized constructor
Test(int cx, int cy)
{
x = cx;
y = cy;
}
// method to add two numbers
void add() { cout << x + y << endl; }
};
int main()
{
// Initializing 3 array Objects with function calls of
// parameterized constructor as elements of that array
Test obj[] = { Test(1, 1), Test(2, 2), Test(3, 3) };

// using add method for each of three elements.


for (int i = 0; i < 3; i++) {
obj[i].add();
}

return 0;
}

4. Using malloc(): To avoid the call of a non-parameterized


constructor, use malloc() method.
#include <iostream>
#define N 5

using namespace std;

class Test {
// private variables
int x, y;

public:
// parameterized constructor
Test(int x, int y)
{
this->x = x;
this->y = y;
}

// function to print
void print() { cout << x << " " << y << endl; }
};

int main()
{
// allocating dynamic array
// of Size N using malloc()
Test* arr = (Test*)malloc(sizeof(Test) * N);

// calling constructor
// for each index of array
for (int i = 0; i < N; i++) {
arr[i] = Test(i, i + 1);
}

// printing contents of array


for (int i = 0; i < N; i++) {
arr[i].print();
}

return 0;
}

5. Create an object of the class which is created dynamically using


the new operator and deleting it explicitly using the delete
operator.

#include <iostream>
using namespace std;

// Class
class Student {

public:
// Constructor
Student()
{
cout << "Constructor is called!\n";
}

// Destructor
~Student()
{
cout << "Destructor is called!\n";
}

// Function to display the message


void write()
{
cout << "Writing!\n";
}
};

// Driver Code
int main()
{
// Create an array of objects
Student* student = new Student();

// Function Call to write()


// using instance
student->write();

// De-allocate the memory


// explicitly
delete student;

return 0;
}
6. C++ program to define a Student class to read and print Student
information using an array of objects
#include<iostream>
using namespace std;
class Student
{
private:
int rno;
char name[20];
float avgmarks;
public:
void read_data();
void print_data();
};
void Student::read_data()
{
cout<<"Enter the roll no. ";
cin>>rno;
cout<<"Enter name. ";
cin>>name;
cout<<"Enter Average Marks. ";
cin>>avgmarks;
}
void Student::print_data()
{
cout<<rno<<"\t"<<name<<"\t"<<avgmarks<<endl;
}
int main()
{
Student std[5];
for (int i=0;i<3;i++)
{
std[i].read_data();
}
cout<<"Student Information is"<<endl;
cout<<"RNo\tName\tAvg Marks"<<endl;
for (int i=0;i<3;i++)
{
std[i].print_data();
}
return 0;
}

1. Write a program in C++ to convert a decimal number into binary


without using an array and using the constructor and destructor.
Decimal 23= Binary 10111

Constructor:

#include <iostream>
using namespace std;
class Decimal_Number
{
public:
Decimal_Number(int n)
{
int i=1,j=n,Binary_Number=0;
cout<<"Please enter Decimal number : ";
cin>>n;
for(j=n;j>0;j=j/2)
{
Binary_Number=Binary_Number+(n
%2)*i;
i=i*10;
n=n/2;
}
cout<<"binary number="<<Binary_Number;
}
~Decimal_Number()
{
cout<<"Destructor run , FREE MEMORY "<<endl;
}
};
int main()
{
Decimal_Number obj(4);
}
2. Write C++ Program to display the cube of the number upto a
given integer using Destructor.
#include<iostream>
#include<conio.h>
using namespace std;
class lst
{
private:
int i,n;
public:
lst()
{
i=1;
n=0;
}
lst(int p, int q)
{
i=p;
n=q;
}
void display()
{
cout<<"enter the number :"<<endl;
cin>>n;
for(i=1; i<=n; i++)
{
cout<<" The cube of"<<i<<"is ="<<(i*i*i)<<endl;

}
}
~lst()
{
cout<<" clear :"<<endl;
}
};
int main()
{
int p,q;
lst obj(p,q);
obj.display();
getch();
}
Please enter the number:

cube of 1 is: 1
cube of 2 is: 8

cube of 3 is: 27

cube of 4 is: 64

3. Write a c++ program to find out the sum of an A.P. series by using
the constructor overloading
#include<iostream>
using namespace std;
class AP_Constructor
{
private :
int n1, AP_Difference, n2, i, ln, s1;
public :
AP_Constructor()
{
s1 = 0;
cout << "Input the starting number of the A.P. series: "<<endl;
cin >> n1;
cout << "Input the number of items for the A.P. series: "<<endl;
cin >> n2;
cout << "Input the common difference of A.P. series: "<<endl;
cin>>AP_Difference;
s1 = (n2 * (2 * n1 + (n2 - 1) * AP_Difference)) / 2;
ln = n1 + (n2 - 1) * AP_Difference;
cout << "The Sum of the A.P. series are : "<<endl;
for (i = n1; i <= ln; i = i + AP_Difference)
{
if (i != ln)
cout << i << " + ";
else
cout << i << " = " << s1 << endl;
}
}
AP_Constructor(int n1, int n2,int AP_Difference)
{
s1 = 0;
s1 = (n2 * (2 * n1 + (n2 - 1) * AP_Difference)) / 2;
ln = n1 + (n2 - 1) * AP_Difference;
cout << "The Sum of the A.P. series are : "<<endl;
for (i = n1; i <= ln; i = i + AP_Difference)
{
if (i != ln)
cout << i << " + ";
else
cout << i << " = " << s1 << endl;
}
}

};
int main()
{
int option;
cout<<"Please Enter the 1 for AP_Constructor with No
paranter :"<<endl;
cout<<"Please Enter the 2 for AP_Constructor with
Paramter : \n";
cout<<"Please Enter the 1 or 2 here : ";
cin>>option;
system("cls");
if(option==1)
{
cout<<"You Have Slected No Paramter AP_Constructoructor.... "<<endl;
AP_Constructor a;

}
else if(option==2)
{
int n1,n2,AP_Difference;
cout << "Input the starting number of the A.P. series: "<<endl;
cin >> n1;
cout << "Input the number of items for the A.P. series: "<<endl;
cin >> n2;
cout << "Input the common difference of A.P. series: "<<endl;
cin>>AP_Difference;
AP_Constructor (n1,n2,AP_Difference);
}
else
{
cout<<"Your Input is Wrong : Try Again."<<endl;
}
}

4. Write a C++ Program to display the reverse of a number using the


constructor.
#include<iostream>
using namespace std;
class Revnum
{
private:
int n,i;
public:
Revnum()
{
cout<<"Enter number to reverse: ";
cin>>n;
}
void out()
{
cout<<endl<<"The reverse of the Entered number: ";
for(i=n;n>0;n=n/10)
{
cout<<n%10;
}
}
};
int main()
{
Revnum r;
r.out();
}

5. Write a C++ Program to display the reverse of a number using the


destructor.
#include<iostream>
using namespace std;
class Revnum
{
private:
int n,i;
public:
Revnum()
{
cout<<"Enter number to Display its reverse: ";
cin>>n;
}
~ Revnum()
{
cout<<endl<<" reverse of the Entered Number is: ";
for(i=n;n>0;n=n/10)
{
cout<<n%10;
}
}
};
int main()
{
Revnum R;
}

Enter number to reverse: 678

The reverse of the Entered number: 876

6. Write a C++ Program to display the reverse of a number using the


constructor overloading.

#include<iostream>
using namespace std;
class Revnum
{
private:
int i;
public:
Revnum(int n)
{

cout<<endl<<"The reverse of the Entered number: ";


for(i=n;n>0;n=n/10)
{
cout<<n%10;
}
}
Revnum(long int a)
{
cout<<endl<<" The reverse of the Entered number: ";
for(i=a;a>0;a=a/10)
{
cout<<a%10;
}
}
};
int main()
{
int choice;
cout<<"Enter choice: ";
cin>>choice;
switch(choice)
{
case 0:
{
int n;
cout<<"Enter a number to Display: ";
cin>>n;
Revnum r(n);
break;
}
case 1:
{
long int a ;
cout<<"Enter Number to Display: ";
cin>>a;
Revnum r(a);
break;
}
default:
cout<<"Invalid Choice:";

}
}
7. Write C++ program to add two complex numbers using Binary
Operator Overloading.

#include <iostream>
using namespace std;

class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}

void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}

// Overload the + operator


Complex operator + (const Complex& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}

void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag
<< "i";
}
};

int main() {
Complex complex1, complex2, result;
cout << "Enter first complex number:\n";
complex1.input();

cout << "Enter second complex number:\n";


complex2.input();

// complex1 calls the operator function


// complex2 is passed as an argument to the function
result = complex1 + complex2;
result.output();

return 0;
}
8. Write C++ program to overload unary operator ++.
#include <iostream>
using namespace std;

class Count {
private:
int value;

public
:
// Constructor to initialize count to 5
Count() : value(5) {}

// Overload ++ when used as prefix


Count operator ++ () {
Count temp;
// Here, value is the value attribute of the calling object
temp.value = ++value;

return temp;
}

// Overload ++ when used as postfix


Count operator ++ (int) {
Count temp;

// Here, value is the value attribute of the calling object


temp.value = value++;

return temp;
}

void display() {
cout << "Count: " << value << endl;
}
};

int main() {
Count count1, result;

// Call the "Count operator ++ ()" function


result = ++count1;
result.display();

// Call the "Count operator ++ (int)" function


result = count1++;
result.display();

return 0;
}

9. WAP to Add members of two different classes using friend


functions.
// Add members of two different classes using friend functions

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {

public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}

private:
int numA;

// friend function declaration


friend int add(ClassA, ClassB);
};

class ClassB {

public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}

private:
int numB;

// friend function declaration


friend int add(ClassA, ClassB);
};

// access members of both classes


int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
}

int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}
10. WAP to Perform simple Calculations With a Friend Class in
C++.
#include <iostream>
using namespace std;
class Exmp_A{
private:
int x;
// Declaring the friend class
friend class Exmp_B;
public:
// Initializing x value using a constructor
Exmp_A() : x(9) {}
};
class Exmp_B{
private:
int y;
public:
// Initializing y value using a constructor
Exmp_B() : y(13) {}
// Function to perform addition
int sum(){
Exmp_A a;
return a.x + y;
}
};
int main(){
Exmp_B b;
cout << "Sum is: " << b.sum();
return 0;
}
11. WAP to Count the number of words in a string
#include <iostream>
#include <string>

using namespace std;

void count_words(string str)


{
/* create variable counter of type integer */
int count = 0;

/* loop through characters in string */


for(int i = 0; i < str.length(); i++)
{
/* check if current char is a space */
if ( str[i] == ' ' )
{
/* in such case, increment the counter */
count++;
}
}

/* increment the counter one last time for the last word */
count++;

cout << "Number of words: " << count << endl;


}

int main()
{
string str = "one two three four";
cout << "Input string: " << str << endl;
count_words(str);

return 0;
}

You might also like