Professional Documents
Culture Documents
Lab Sheet 03
Lab Sheet 03
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);
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)};
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) };
return 0;
}
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);
}
return 0;
}
#include <iostream>
using namespace std;
// Class
class Student {
public:
// Constructor
Student()
{
cout << "Constructor is called!\n";
}
// Destructor
~Student()
{
cout << "Destructor is called!\n";
}
// Driver Code
int main()
{
// Create an array of objects
Student* student = new 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;
}
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;
}
}
#include<iostream>
using namespace std;
class Revnum
{
private:
int i;
public:
Revnum(int n)
{
}
}
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;
}
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();
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) {}
return temp;
}
return temp;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1, result;
return 0;
}
#include <iostream>
using namespace std;
// forward declaration
class ClassB;
class ClassA {
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
private:
int numA;
class ClassB {
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
private:
int 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>
/* increment the counter one last time for the last word */
count++;
int main()
{
string str = "one two three four";
cout << "Input string: " << str << endl;
count_words(str);
return 0;
}