0% found this document useful (0 votes)
3 views16 pages

Constructors Based Examples

The document provides a series of C++ programming exercises focusing on class creation, object initialization, and memory management. Each exercise includes a problem statement followed by a solution that demonstrates various concepts such as constructors, destructors, copy constructors, static members, and inline functions. The examples illustrate how to manage object data and behavior effectively in C++.

Uploaded by

newsairam2008
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Constructors Based Examples

The document provides a series of C++ programming exercises focusing on class creation, object initialization, and memory management. Each exercise includes a problem statement followed by a solution that demonstrates various concepts such as constructors, destructors, copy constructors, static members, and inline functions. The examples illustrate how to manage object data and behavior effectively in C++.

Uploaded by

newsairam2008
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Questions in C++:

1) Automatic Initialization

Create a class named Student with a data member called name.

When an object of the class is created:

 It should automatically store the name "Unknown"

 Display the message "Object created"

 Print the stored name in the main function.

Solution:

#include <iostream>

using namespace std;

class Student

public:

string name;

Student()

cout<<”Object Created”;

name = "Unknown";

};

int main()

Student s1;

cout << "Name: " << [Link];

}
2) Setting Initial Bank Balance

Design a class named BankAccount that contains:

 A private data member called balance.

Requirements:

 When an object is created, the balance should automatically be set


to ₹1000.

 Provide a public function to display the balance.

Solution:

#include <iostream>

using namespace std;

class BankAccount

private:

int balance;

public:

BankAccount()

balance = 1000;

void showBalance()

cout << "Balance: " << balance;

};

int main()

BankAccount b1;

[Link]();

}
3) Storing Student Details at Creation

Create a class named Student that stores:

 Name

 Age

Requirements:

 The values must be provided at the time of creating the object.

 Display the student details.

Solution:

#include <iostream>

using namespace std;

class Student

public:

string name;

int age;

Student(string n, int a)

name = n;

age = a;

};

int main()

Student s1("Ravi", 18);

cout << [Link] << " " << [Link];

4) Different Ways to Create Objects


Create a class Demo.

Requirements:

If an object is created without input, display:


"Default object created"
If created with a number, display:
"Object created with value: <number>"

Solution:

#include <iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout << "Default object created\n";
}

Demo(int x)
{
cout << "Object created with value: " << x;
}
};

int main()
{
Demo d1;
Demo d2(10);
}

5) Copying Object Data


Create a class Number with an integer data member value.

Requirements:

Initialize the first object with a number.


Create another object that automatically gets the same value.
Display the second object's value.

Solution:

#include <iostream>
using namespace std;
class Number
{
public:
int value;

Number(int v)
{
value = v;
}
Number(const Number &n)
{
value = [Link];
}
};
int main()
{
Number n1(5);
Number n2 = n1;

cout << "Value: " << [Link];


}

6) Restricting Direct Object Creation

Design a class Test where:

Objects should NOT be created directly in the main function.


Provide a public function inside the class that creates an object and
displays a message.
Solution:

#include <iostream>

using namespace std;

class Test

private:

Test()

cout << "Object created inside class";

public:

static void createObject()

Test t;

};

int main()

Test::createObject();

7) Using Private Data with Initialization

Create a class Employee where:


 Salary is private.
 It should automatically be set to ₹20000 when an object is created.
 Display it using a public function.

Solution:
#include <iostream>
using namespace std;

class Employee
{
private:
int salary;

public:
Employee()
{
salary = 20000;
}

void display()
{
cout << "Salary: " << salary;
}
};

int main()
{
Employee e1;
[Link]();
}

Destructor:
Constructor = Allocate resources
Destructor = Release resources

#include <iostream>
using namespace std;

class Student
{
private:
static int count;

public:
Student()
{
count++;
cout << "Student entered. Count: " << count << endl;
}

~Student()
{
count--;
cout << "Student left. Count: " << count << endl;
}
};

int Student::count = 0;

int main()
{
Student s1, s2;
}

Example 2:
#include <iostream>
using namespace std;

class LoginSession
{
public:
LoginSession()
{
cout << "User logged in\n";
}

~LoginSession()
{
cout << "User logged out\n";
}
};

int main()
{
LoginSession user;
}

Example 3:

#include <iostream>
using namespace std;

class Number
{
private:
int *ptr;

public:
Number()
{
ptr = new int;
*ptr = 50;
cout << "Memory allocated\n";
}

~Number()
{
delete ptr;
cout << "Memory freed\n";
}
};

int main()
{
Number n;
}

Example 4:
#include <iostream>
#include <fstream>
using namespace std;

class FileHandler
{
private:
ofstream file;

public:
FileHandler()
{
[Link]("[Link]");
cout << "File opened\n";
}

void write()
{
file << "Hello\n";
}

~FileHandler()
{
[Link]();
cout << "File closed\n";
}
};

int main()
{
FileHandler f; // constructor runs
[Link](); // writing happens
return 0; // destructor runs automatically
}

Copy Constructor and this:


#include <iostream>
using namespace std;
class Student
{
private:
int marks;
public:
// Parameterized constructor
Student(int m)
{
marks = m;
}
// Copy constructor using this pointer
Student(const Student &s)
{
this->marks = [Link];
cout << "Copy constructor called\n";
}
void show()
{
cout << "Marks: " << marks << endl;
}
};
int main()
{
Student s1(90);
Student s2 = s1; // Copy constructor called
[Link]();
}

Example 2:
#include <iostream>
using namespace std;

class Student
{
public:
int marks;

Student(int m)
{
marks = m;
}
// Copy constructor
Student(const Student &s)
{
this->marks = [Link];

cout << "Address of original object (s1): " << &s << endl;
cout << "Address of new object (s2 using this): " << this <<
endl;
}
};
int main()
{
Student s1(90);
cout << "Address of s1 in main: " << &s1 << endl;
Student s2 = s1;
cout << "Address of s2 in main: " << &s2 << endl;
}

Static data members and member functions:


A static data member is a variable that:
Is shared by all objects of the class
Has only one copy in memory
Does NOT belong to any single object

Example

#include <iostream>
using namespace std;
class Student
{
public:
static int count; // static data member
Student()
{
count++;
}
};

// Definition of static member


int Student::count = 0;
int main()
{
Student s1, s2, s3;
cout << "Total objects created: " << Student::count;
}

Static Data Member


A static data member is a class variable shared by all objects with
only one copy in memory.

Static Member Function


A static member function belongs to the class and can access only
static members.

Example 3
#include <iostream>
using namespace std;
class Student
{
public:
static int count;
Student()
{
count++;
}
static void showCount()
{
cout << "Total objects: " << count;
}
};
int Student::count = 0;
int main()
{
Student s1, s2;
Student::showCount(); // called using class name
}

Example 2:
#include <iostream>
using namespace std;
class Library
{
private:
int booksIssued; // individual data
static int totalBooks; // shared by all objects
public:
Library(int b)
{
booksIssued = b;
totalBooks += b;
}

void showStudentData()
{
cout << "Books issued to student: " << booksIssued << endl;
}

static void showTotalBooks()


{
cout << "Total books issued in library: " << totalBooks <<
endl;
}
};

// Definition of static member


int Library::totalBooks = 0;

int main()
{
Library s1(2);
Library s2(3);
Library s3(1);
[Link]();
[Link]();
[Link]();
Library::showTotalBooks();
}

Constant Member functions:


#include <iostream>
using namespace std;
class Student
{
public:
int marks = 90;
void show()
{
cout << marks;
}
};

int main()
{
const Student s;
[Link](); // ERROR
}

Corrected version:
#include <iostream>
using namespace std;
class Student
{
public:
int marks = 90;
void show() const // constant member function
{
cout << "Marks: " << marks;
}
};

int main()
{
const Student s;
[Link](); // ✅ Allowed
}

Normal Constant
Feature
Function Function

Can modify data Yes No

Can be called by const


No Yes
object

Has const keyword No Yes

Inline functions:
Automatic inline
#include <iostream>
using namespace std;

class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
};

int main()
{
Calculator c;
cout << [Link](5, 3);
}

Normal function: Every call → jump → return.


Inline function: No jump — code directly inserted.

Explicit inline
#include <iostream>
using namespace std;

class Test
{
public:
void show(); // function declaration
};

// Inline function defined outside class


inline void Test::show()
{
cout << "Inline function";
}

int main()
{
Test t; // create object
[Link](); // call inline member function
return 0;
}

You might also like