Parameterized Constructor Example in C++


This program creates a class Student which has three data members - ID, Name, and Percentage. It has one constructor that takes three arguments - i, n, and s - which are used to initialize the data members. The class also has a display() function which prints out the values of ID, Name, and Percentage. In the main() function, two objects of the Student class are created - s1 and s2 - using the constructor with three arguments. The display() function is then called for each object to print out the values of its data members.

Source Code

#include <iostream>
using namespace std;
class Student
{
   public:
    int ID;
    string Name;
    float Percentage;
    Student(int i, string n, float s)
    {
      ID=i;
      Name=n;
      Percentage=s;
    }
    void display()
    {
       cout<<ID<<"  "<<Name<<"  "<<Percentage<<endl;
    }
};
int main(void)
{
  Student s1=Student(101, "Arun", 93);
  Student s2=Student(102, "Aswin", 89);
  s1.display();
  s2.display();
  return 0;
}
To download raw file Click Here

Output

101  Arun  93
102  Aswin  89

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples