0% found this document useful (0 votes)
6 views10 pages

C++ Class and Constructor Examples

The document contains multiple C++ code examples demonstrating key object-oriented programming concepts such as encapsulation, inheritance, function overloading, operator overloading, and constructors (default, parameterized, copy). It illustrates the use of classes and objects, including the implementation of shallow and deep copy constructors, as well as resolving ambiguity in multiple inheritance. Each example is accompanied by a brief explanation of its functionality.

Uploaded by

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

C++ Class and Constructor Examples

The document contains multiple C++ code examples demonstrating key object-oriented programming concepts such as encapsulation, inheritance, function overloading, operator overloading, and constructors (default, parameterized, copy). It illustrates the use of classes and objects, including the implementation of shallow and deep copy constructors, as well as resolving ambiguity in multiple inheritance. Each example is accompanied by a brief explanation of its functionality.

Uploaded by

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

#include <iostream>

using namespace std;


class Person {
private:
int age;

public:
// Setter function
void setAge(int a) {
if (a >= 0) // validation check
age = a;
else
cout << "Invalid age!" << endl;
}

// Getter function
int getAge() {
return age;
}
};

int main(){
Person p;
[Link](25); // ✅ Safe way to set value
cout << [Link]() << endl; //  Safe way to access value

return 0;
}
#include <iostream>
using namespace std;

// Base class (Parent)


class Animal {
public:
void eat() {
cout << "Eating food" << endl;
}
};

// Derived class (Child)


class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking" << endl;
}
};

int main() {
Dog d;
[Link](); // Inherited from Animal
[Link](); // Dog's own function

return 0;
}
7 a) Function Overloading

#include <iostream>
using namespace std;

class Print {
public:
void show(int x) {
cout << "Integer: " << x << endl;
}
void show(string s) {
cout << "String: " << s << endl;
}
};

int main() {
Print p;
[Link](10);
[Link]("Hello");
return 0;
}
Operator overloading

#include <iostream>
using namespace std;

class Number {
public:
int value;

// Constructor to set the value


Number(int v) {
value = v;
}

// Overload the + operator


Number operator + (Number obj) {
Number result(0);
[Link] = value + [Link];
return result;
}

// Function to display the value


void show() {
cout << "Value: " << value << endl;
}
};

int main() {
Number a(5); // [Link] = 5
Number b(10); // [Link] = 10

Number c = a + b; // Calls operator+ function

[Link](); // Output: Value: 15

return 0;
}
#include <iostream>
using namespace std;

// Base class
class Animal {
public:
virtual void speak() {
cout << "Animal speaks" << endl;
}
};

// Derived class
class Dog : public Animal {
public:
void speak() override {
cout << "Dog barks" << endl;
}
};

// Another Derived class


class Cat : public Animal {
public:
void speak() override {
cout << "Cat meows" << endl;
}
};

int main() {
Dog d;
Cat c;

// Pointer of base class type


Animal* a;

// Pointing to Dog object


a = &d;
a->speak(); // Calls Dog's speak(): "Dog barks"

// Pointing to Cat object


a = &c;
a->speak(); // Calls Cat's speak(): "Cat meows"

return 0;
}
Default Constructor
#include <iostream>
using namespace std;
class Person {
public:
int age;

Person() {
age = 0;
cout << "Default constructor called\n";
}
};

Parameterized constructor
#include <iostream>
using namespace std;
class Person {
public:
int age;

Person(int a) {
age = a;
cout << "Parameterized constructor called\n";
}
};

Copy constructor

#include <iostream>
using namespace std;
class Person {
public:
int age;

Person(int a) {
age = a;
}

// Copy constructor
Person(const Person &p) {
age = [Link];
cout << "Copy constructor called\n";
}
};
Person p1(30);
Person p2 = p1; // Calls copy constructor

Shallow copy constructor

class Person {
public:
int* age;

Person(int a) {
age = new int(a);
}

// Shallow Copy Constructor (bad practice here)


Person(const Person &p) {
age = [Link]; // Shallow copy (both point to same memory)
cout << "Copy constructor called\n";
}

void show() {
cout << "Age: " << *age << endl;
}

~Person() {
delete age;
}
};

int main() {
Person p1(30);
Person p2 = p1; // Shallow copy

*[Link] = 500; // Modifying p2's age

// p1 is also affected because both share the same memory


[Link](); // Output: Age: 500

return 0;
}
Deep Copy constructor

class Person {
public:
int* age;

Person(int a) {
age = new int(a);
}

// Shallow Copy Constructor (bad practice here)


Person(const Person &p) {
age = new int(*[Link]); // Shallow copy (both point to same memory)
cout << "Copy constructor called\n";
}

void show() {
cout << "Age: " << *age << endl;
}

~Person() {
delete age;
}
};

int main() {
Person p1(30);
Person p2 = p1; // Shallow copy

*[Link] = 500; // Modifying p2's age

// p1 is also affected because both share the same memory


[Link](); // Output: Age: 500

return 0;
}
#include <iostream>
using namespace std;

class A {
public:
void greet() {
cout << "Hello from A\n";
}
};

class B : public A { };
class C : public A { };

// D inherits from both B and C


class D : public B, public C { };

int main() {
D obj;

// [Link](); // ❌ Error: Ambiguous — which A's greet() to call?

//5 To resolve manually, you have to specify the path


obj.B::greet(); // Calls greet() from A via B


obj.C::greet(); // Calls greet() from A via C




return 0;
}

You might also like