Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to make an overloaded constructor in c++

// C++ program to demonstrate constructor overloading
#include <iostream>
using namespace std;

class Person {
   private:
    int age;

   public:
    // 1. Constructor with no arguments
    Person() {
        age = 20;
    }

    // 2. Constructor with an argument
    Person(int a) {
        age = a;
    }

    int getAge() {
        return age;
    }
};

int main() {
    Person person1, person2(45);

    cout << "Person1 Age = " << person1.getAge() << endl;
    cout << "Person2 Age = " << person2.getAge() << endl;

    return 0;
}
Comment

constructor overloading in c++

constructor oveloading in c++
  
Comment

PREVIOUS NEXT
Code Example
Cpp :: detect end of user input cpp 
Cpp :: C++ String Length Example 
Cpp :: naive pattern matching algorithm 
Cpp :: declare nullptr c++ 
Cpp :: c++ string element access 
Cpp :: checking if a string has only letters cpp 
Cpp :: how to declare a 2d boolean vector in c++ 
Cpp :: c++ cstring to string 
Cpp :: cpp get last element of vector 
Cpp :: int main() { 
Cpp :: To Lower Case leetcode solution in c++ 
Cpp :: int to float c++ 
Cpp :: min in c++ 
Cpp :: ascii conversion cpp 
Cpp :: to lowercase c++ 
Cpp :: function in struct c++ 
Cpp :: c++ filesystem read directory 
Cpp :: how to empty string c++ 
Cpp :: c pre-processor instructions 
Cpp :: print two dimensional array c++ 
Cpp :: zero fill in c++ 
Cpp :: how to cout in c++ 
Cpp :: binary search in c++ 
Cpp :: c++ auto 
Cpp :: how to create a file in c++ 
Cpp :: convert all strings in vector to lowercase or uppercase c++ 
Cpp :: calculator in cpp 
Cpp :: selection sort c++ 
Cpp :: Translation codeforces in c++ 
Cpp :: cpp oop 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =