[Link]
in/cpp_programming_tutorial
[Link] data and member function
#include<iostream.h>
#include<conio.h>
class account {
private:
int acc_no; double balance;
static double rate; //static data member declaration
public:
void read() {
cout<<"\nEnter account no. and balance = ";
cin>>acc_no>>balance;
}
void show{) {
cout<<"\nAccount no = "<<acc_no<< '\t';
cout<<''Interest = "<<rate<<'\t';
cout<<''Balance ="<<balance;
}
void qtr_rate_cal() {
double interest=(balance*rate*0.25);
balance=balance+interest;
}
};
double account::rate=0.05; //static data member definition
int main() {
clrscr();
account a1,a2;
cout<<"Enter customer 1 information ..... \n";
[Link]();
cout<<"Enter customer 2 information ..... \n";
[Link]();
a1.qtr_rate_cal();
a2.qtr_rate_cal();
[Link]();
[Link]();
getch();
return 0;
}
Output :
Enter customer 1 information .....
Enter account no. and balance = 201 1000
Enter customer 2 information .....
Enter account no. and balance = 202 2000
Account No. = 201 Interest= 0.05 Balance = 1012.5
Account No. = 202 Interest = 0.05 Balance = 2025
2.A program to demonstrate the concept of array of objects
#include<iostream>
using namespace std;
class books {
char tit1e [30];
float price ;
public:
void getdata ();
void putdata ();
};
void books :: getdata () {
cout<<"Title:”;
cin>>title;
cout<<"Price:”;
cin>>price;
}
void books :: putdata () {
cout<<"Title:"<<title<< "\n";
cout<<"Price:"<<price<< "\n”;
const int size=3 ;
}
int main() {
books book[size] ;
for(int i=0;i<size;i++) {
cout<<"Enter details o£ book "<<(i+1)<<"\n";
book[i].getdata();
}
for(int i=0;i<size;i++) {
cout<<"\nBook "<<(i+l)<<"\n";
book[i].putdata() ;
}
return 0;
}
The output of the program is
Enter details of book 1
Title: c++
Price: 325
Enter details of book 2
Title: DBMS
Price:. 455
Enter details of book 3
Title: Java
Price: 255
Book 1
Title: c++
Price: 325
Book 2
Title: DBMS
Price: 455
Book 3
Title: Java
Price: 255
[Link] and Destructor
#include <iostream>
using namespace std;
class student
{
private: char name[20],add[20];
int roll,zip;
public: student ( ); //Constructor
~student( ); //Destructor
void read( );
void disp( );
};
student :: student( )
{
cout<<"Student class constructor called."<<endl;
}
void student :: read( )
{
cout<<"Enter the student Name: ";
cin>>name;
cout<<"Enter the student roll no: “;
cin>>roll;
cout<<"Enter the student address: ";
cin>>add;
cout<<"Enter the Zipcode: ";
cin>>zip;
}
void student :: disp( )
{
cout<<"Studet details"<<endl;
cout<<"Student Name :"<<name<<endl;
cout<<"Roll no is :"<<roll<<endl;
cout<<"Address is :"<<add<<endl;
cout<<"Zipcode is :"<<zip<<endl;
}
student :: ~student( )
{
cout<<"Student class destructor called.";
}
int main( )
{
student s;
[Link]();
[Link]();
}
/* C++ program to Display Student Details using constructor and destructor */
#include<iostream>
using namespace std;
class stu
{
private:
char name[20],add[20];
int roll,zip;
public:
stu ();//Constructor
~stu();//Destructor
void read();
void disp();
};
stu :: stu()
{
cout<<"\nThis is Student Details constructor called..........."<<endl;
}
void stu :: read()
{
cout<<"\nEnter the student Name :: ";
cin>>name;
cout<<"\nEnter the student roll no :: ";
cin>>roll;
cout<<"\nEnter the student address :: ";
cin>>add;
cout<<"\nEnter the Zipcode :: ";
cin>>zip;
}
void stu :: disp()
{
cout<<"\nThe Entered Student Details are shown below ::---------- \n";
cout<<"\nStudent Name :: "<<name<<endl;
cout<<"\nRoll no is :: "<<roll<<endl;
cout<<"\nAddress is :: "<<add<<endl;
cout<<"\nZipcode is :: "<<zip;
}
stu :: ~stu()
{
cout<<"\n\nStudent Detail is Closed.............\n";
}
int main()
{
stu s;
[Link] ();
[Link] ();
return 0;
Output:
}
/* C++ program to Display Student Details using constructor and destructor */
This is Student Details constructor called...........
Enter the student Name :: CodezClub
Enter the student roll no :: 123
Enter the student address :: India
Enter the Zipcode :: 100215
The Entered Student Details are shown below ::----------
Student Name :: CodezClub
Roll no is :: 123
Address is :: India
Zipcode is :: 100215
Student Detail is Closed.............
Process returned 0
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *nm;
int len;
public:
string(char *s)
{
len=strlen(s);
nm=new char[len+ 1];
strcpy(nm,s);
}
void com(string &ss2)
{
int k;
k=strcmp(nm,[Link]);
if(k==0)
cout<<"\nBoth objects have same name";
else
cout<<"\nBoth objects have different name";
}
void put()
{
cout<<"\nName of person : "<<nm;
}
~string()
{
cout<<"\nRelease memory allocated to"<<nm;
delete[] nm;
}
};
void main()
{
clrscr();
string obj1 ("Dinesh");
string obj2("Thakur");
[Link]();
[Link]();
[Link](obj2);
getch();
}
4. Employee Details By Using Class and Object
#include<iostream.h>
#include<conio.h>
class emp
{
private:
char ename[30];
int eid;
long int salary,da,hra,pf,lic,loan,gp,ded,netpay;
public:
void getdata();
void caldata();
void printdata();
};
void emp::getdata()
{
cout<<"Enter the Employee Name:"<<endl;
cin>>ename;
cout<<"Enter the Employee Id Number:"<<endl;
cin>>eid;
cout<<"Enter the Basic Salary:"<<endl;
cin>>salary;
}
void emp::caldata()
{
da=salary*5/100;
hra=salary*8/100;
pf=salary*12/100;
lic=salary*2/100;
loan=salary*1/100;
gp=salary+da+hra;
ded=pf+lic+loan;
netpay=gp-ded;
}
void emp::printdata()
{
cout<<"**************************************************"<<endl;
cout<<"EMPLOYEE PAYSLIP FOR THE MONTH OF April 2024"<<endl;
cout<<"**************************************************"<<endl;
cout<<"EMPLOYEE NAME :"<<ename<<endl;
cout<<"EMPLOYEE ID NUMBER :"<<eid<<endl;
cout<<"BASIC SALARY :"<<salary<<endl;
cout<<"DA :"<<da<<endl;
cout<<"HRA :"<<hra<<endl;
cout<<"PF :"<<pf<<endl;
cout<<"LIC :"<<lic<<endl;
cout<<"LOAN :"<<loan<<endl;
cout<<"GROSS PAY :"<<gp<<endl;
cout<<"DEDUCTION :"<<ded<<endl;
cout<<"NET PAY :"<<netpay<<endl;
cout<<"**************************************************"<<endl;
}
void main()
{
emp e;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
Function Overloading
// function overloading
#include<iostream.h>
#include<conio.h>
void sum_avg(int, int);
void sum_avg(int);
void sum_avg(int, int, int);
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the A & B Values:"<<endl;
cin>>a>>b;
sum_avg(a,b);
sum_avg(100);
cout<<"Enter the A, B & C Values:"<<endl;
cin>>a>>b>>c;
sum_avg(a,b,c);
getch();
}
void sum_avg(int a, int b)
{
int sum;
float avg;
sum=a+b;
avg=sum/2;
cout<<"Sum of two numbers are."<<sum<<endl;
cout<<"Average value is."<<avg<<endl;
}
void sum_avg(int x)
{
x=a;
Cout<<”a=”<<x;
}
void sum_avg(int a, int b , int c)
{
int sum;
float avg;
sum=a+b+c;
avg=sum/3;
cout<<"Sum of three numbers are: "<<sum<<endl;
cout<<"Average value is :"<<avg<<endl;
}
5./ Numeric Operation with Inline Function
#include<iostream.h>
#include<conio.h>
inline int add(int x, int y)
{
return x+y;
}
inline int mul(int p, int q)
{
return p*q;
}
inline double div(int m , int n)
{
return m/n;
}
void main()
{
int x,y;
clrscr();
cout<<"Enter the Values :\n";
cin>>x>>y;
cout<<"Sum ="<<add(x,y)<<endl;
cout<<"Multiplication = "<<mul(x,y)<<endl;
cout<<"Division = "<<div(x,y)<<endl;
getch();
}
[Link] Function
#include<iostream.h>
#include<conio.h>
class mark;
class info
{
private:
char name[30];
int reg;
public:
void getdata()
{
cout<<"Enter the name:\n";
cin>>name;
cout<<"Enter the register:\n";
cin>>reg;
}
friend void print(info f, mark k);
};
class mark
{
private:
int m1,m2,m3;
public:
void getdata()
{
cout<<"Enter the marks:\n";
cin>>m1>>m2>>m3;
}
friend void print(info f, mark k);
};
void print(info f, mark k)
{
cout<<"Name :"<<[Link]<<endl;
cout<<"Register number :"<<[Link]<<endl;
cout<<"Mark 1 :"<<k.m1<<endl;
cout<<"Mark 2 :"<<k.m2<<endl;
cout<<"Mark 3 :"<<k.m3<<endl;
cout<<"Total Mark :"<<k.m1+k.m2+k.m3<<endl;
if(k.m1>=40 && k.m2>=40 && k.m3>=40)
cout<<"Result is pass"<<endl;
else
cout<<"Result is fail"<<endl;
}
void main()
{
info f1;
mark k1;
clrscr();
[Link]();
[Link]();
print(f1,k1);
getch();
}
[Link] Function
#include<iostream>
using namespace std;
class vaccine
{
public:
virtual void putVaccine()
{
cout<<"Covid Vaccine"<<endl;
}
};
class covaxin:public vaccine
{
public:
void putVaccine()
{
cout<<"Put Covaxin Vaccine"<<endl;
}
};
class covishield:public vaccine
{
public:
void putVaccine()
{
cout<<"Put Covishield Vaccine"<<endl;
}
};
int main()
{
covaxin cx;
covishield cs;
vaccine *o=NULL;
o=&cx;
o->putVaccine();
o=&cs;
o->putVaccine();
return 0;
}
Output
Put Covaxin Vaccine
Put Covishield Vaccine
[Link] Management System using Class & inheritance in C++
.
#include<iostream>
using namespace std;
[Link] Account
[Link] Account
Account Creation
Deposit
Withdraw
Balance
*/
class account
{
private:
string name;
int accno;
string atype;
public:
void getAccountDetails()
{
cout<<"\nEnter Customer Name : ";
cin>>name;
cout<<"Enter Account Number : ";
cin>>accno;
cout<<"Enter Account Type : ";
cin>>atype;
}
void displayDetails()
{
cout<<"\n\nCustomer Name : "<<name;
cout<<"\nAccount Number : "<<accno;
cout<<"\nAccount Type : "<<atype;
}
};
class current_account : public account
{
private:
float balance;
public:
void c_display()
{
cout<<"\nBalance :"<<balance;
}
void c_deposit()
{
float deposit;
cout<<"\nEnter amount to Deposit : ";
cin>>deposit;
balance = balance + deposit;
}
void c_withdraw()
{
float withdraw;
cout<<"\n\nBalance : "<<balance;
cout<<"\nEnter amount to be withdraw :";
cin>>withdraw;
if(balance > 1000)
{
balance=balance-withdraw;
cout<<"\nBalance Amount After Withdraw: "<<balance;
}
else
{
cout<<"\n Insufficient Balance";
}
};
class saving_account : public account
{
private:
float sav_balance;
public:
void s_display()
{
cout<<"\nBalance : "<<sav_balance;
}
void s_deposit()
{
float deposit,interest;
cout<<"\nEnter amount to Deposit : ";
cin>>deposit;
sav_balance = sav_balance + deposit;
interest=(sav_balance*2)/100;
sav_balance=sav_balance+interest;
}
void s_withdraw()
{
float withdraw;
cout<<"\nBalance :- "<<sav_balance;
cout<<"\nEnter amount to be withdraw : ";
cin>>withdraw;
if(sav_balance > 500)
{
sav_balance=sav_balance-withdraw;
cout<<"\nBalance Amount After Withdraw: "<<sav_balance;
}
else
{
cout<<"\n Insufficient Balance";
}
}
};
int main()
{
current_account c1;
saving_account s1;
char type;
cout<<"\nEnter S for saving customer and C for current a/c customer : ";
cin>>type;
int choice;
if(type=='s' || type=='S')
{
[Link]();
while(1)
{
cout<<"\nChoose Your Choice"<<endl;
cout<<"1) Deposit"<<endl;
cout<<"2) Withdraw"<<endl;
cout<<"3) Display Balance"<<endl;
cout<<"4) Display with full Details"<<endl;
cout<<"5) Exit"<<endl;
cout<<"Enter Your choice: ";
cin>>choice;
switch(choice)
{
case 1 :
s1.s_deposit();
break;
case 2 :
s1.s_withdraw();
break;
case 3 :
s1.s_display();
break;
case 4 :
[Link]();
s1.s_display();
break;
case 5 :
goto end;
default:
cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
else if(type=='c' || type=='C')
{
[Link]();
while(1)
{
cout<<"\nChoose Your Choice"<<endl;
cout<<"1) Deposit"<<endl;
cout<<"2) Withdraw"<<endl;
cout<<"3) Display Balance"<<endl;
cout<<"4) Display with full Details"<<endl;
cout<<"5) Exit"<<endl;
cout<<"Enter Your choice: ";
cin>>choice;
switch(choice)
{
case 1 :
c1.c_deposit();
break;
case 2 :
c1.c_withdraw();
break;
case 3 :
c1.c_display();
break;
case 4 :
[Link]();
c1.c_display();
break;
case 5 :
goto end;
default:
cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
else
{
cout<<"\nInvalid Account Selection";
}
end:
cout<<"\nThank You for Banking with us..";
return 0;
}
Output
Enter S for saving customer and C for current a/c customer : s
Enter Customer Name : Siva
Enter Account Number : 123
Enter Account Type : Saving
Choose Your Choice
1) Deposit
2) Withdraw
3) Display Balance
4) Display with full Details
5) Exit
Enter Your choice: 1
Enter amount to Deposit :
1500
Choose Your Choice
1) Deposit
2) Withdraw
3) Display Balance
4) Display with full Details
5) Exit
Enter Your choice: 3
Balance : 1530
Choose Your Choice
1) Deposit
2) Withdraw
3) Display Balance
4) Display with full Details
5) Exit
Enter Your choice: 4
Customer Name : Siva
Account Number : 123
Account Type : Saving
Balance : 1530
Choose Your Choice
1) Deposit
2) Withdraw
3) Display Balance
4) Display with full Details
5) Exit
Enter Your choice: 2
Balance :- 1530
Enter amount to be withdraw : 500
Balance Amount After Withdraw: 1030
Choose Your Choice
1) Deposit
2) Withdraw
3) Display Balance
4) Display with full Details
5) Exit
Enter Your choice: 5
[Link] overloading
#include<iostream>
using namespace std;
class complex
{
int r,i;
public:
complex()
{
r=0,i=0;
}
complex(int a,int b)
{
r=a,i=b;
}
void get();
void display();
friend complex operator+(complex c1,complex c2);
friend complex operator-(complex c1,complex c2);
complex operator*(complex c1);
complex operator/(complex c1);
};
void complex::get()
{
cout<<“\nEnter real no.:\n”;
cin>>r;
cout<<“\nEnter [Link]:\n”;
cin>>i;
}
void complex::display()
{
cout<<r<<“+”<<i<<“i”;
}
complex operator+(complex c1,complex c2)
{
return complex((c1.r+c2.r),(c1.i+c2.i));
}
complex operator-(complex c1,complex c2)
{
return complex((c1.r-c2.r),(c1.i-c2.i));
}
complex complex::operator*(complex c1)
{
return complex(((r*c1.r)-(i*c1.i)),((r*c1.i)+(i*c1.r)));
}
complex complex::operator/(complex x)
{
return complex(((r*x.r)+(i*x.i))/((x.r*x.r)+(x.i*x.i)),((i*x.r)-(r*x.i))/((x.r*x.r)+(x.i*x.i)));
}
int main()
{
int ch;
complex c1,c2,c3;
do
{
cout<<“\nMenu\[Link]\[Link]\[Link]\[Link]\nExit\nEnter choice:”;
cin>>ch;
switch(ch)
{
case 1:[Link]();
[Link]();
c3=operator+(c1,c2);
[Link]();
break;
case 2:[Link]();
[Link]();
c3=operator-(c1,c2);
[Link]();
case 3:[Link]();
[Link]();
c3=c1*c2;
[Link]();
break;
case 4:[Link]();
[Link]();
c3=c1/c2;
[Link]();
break;
break;
}
}while(ch<5);
}
#include<iostream>
#include<stdio.h>
using namespace std;
class complex
{
int real;
float image;
public:
void getdata()
{
cout<<"\n enter the real part of the complex :: ";
cin>>real;
cout<<"\n enter the imaginary part of the complex :: ";
cin>>image;
}
void operator + (complex);
void operator - (complex);
};
void complex :: operator + (complex c1)
{
complex temp;
[Link]=real+[Link];
[Link]=image+[Link];
if ([Link]>=0)
{
cout<<"\n complex no. after addition :: ";
cout<<[Link]<<"+"<<[Link]<<"i\n";
}
else
{
cout<<"\n complex no. after addition :: ";
cout<<[Link]<<[Link]<<"i\n";
}
}
void complex ::operator-(complex c1)
{
complex temp;
[Link] = [Link];
[Link]= [Link];
if ([Link]>=0)
{
cout<<"\n complex no. after subtraction :: ";
cout<<"\n [Link]"<<"+"<<[Link]<<"i\n";
}
else
{
cout<<"\n complex no. after subtraction :: ";
cout<<[Link]<<[Link]<<"i\n";
}
}
int main()
{
complex c1, c2;
int n;
do
{
cout<<"\n 1. Input data for complex no. ";
cout<<"\n 2. Addition of complex no. ";
cout<<"\n 3. Subtraction of complex no. ";
cout<<"\n 4. Quit";
cout<<"\n Enter your choice :: ";
cin>>n;
switch(n)
{
case 1:
cout<<endl<<"\n Enter the data for First Complex No......";
[Link]();
cout<<endl<<"\n Enter the data for seconds Complex No.....";
[Link]();
break;
case 2:
c1+c2;
break;
case 3:
c1-c2;
break;
case 4:
exit(1);
break;
}
} while (n!=4);
return 0;
1. Input data for complex no.
2. Addition of complex no.
3. Subtraction of complex no.
4. Quit
Enter your choice :: 1
Enter the data for First Complex No......
enter the real part of the complex :: 2
enter the imaginary part of the complex :: 3
Enter the data for seconds Complex No.....
enter the real part of the complex :: 4
enter the imaginary part of the complex :: 5
1. Input data for complex no.
2. Addition of complex no.
3. Subtraction of complex no.
4. Quit
Enter your choice :: 2
complex no. after addition :: 6+8i
1. Input data for complex no.
2. Addition of complex no.
3. Subtraction of complex no.
4. Quit
Enter your choice :: 3
complex no. after subtraction :: -3-2i
1. Input data for complex no.
2. Addition of complex no.
3. Subtraction of complex no.
4. Quit
Enter your choice :: 4
Process returned 1
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class complex
{
int i,r;
public:
void read()
{
cout<<"\nEnter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void display()
{
cout<<"\n= "<<r<<"+"<<i<<"i";
}
complex operator+(complex a2)
{
complex a;
a.r=r+a2.r;
a.i=i+a2.i;
return a;
}
complex operator-(complex a2)
{
complex a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex operator*(complex a2)
{
complex a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
complex operator/(complex a2)
{
complex a;
a.r=((r*a2.r)+(i*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
a.i=((i*a2.r)-(r*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
return a;
}
};
void main()
{
int ch;
clrscr();
complex a,b,c;
do
{
cout<<"\[Link] [Link]";
cout<<" [Link] [Link] [Link]\n";
cout<<"\nEnter the choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter The First Complex Number:";
[Link]();
[Link]();
cout<<"\nEnter The Second Complex Number:";
[Link]();
[Link]();
c=a+b;
[Link]();
break;
case 2:
cout<<"\nEnter The First Complex Number:";
[Link]();
[Link]();
cout<<"\nEnter The Second Complex Number:";
[Link]();
[Link]();
c=b-a;
[Link]();
break;
case 3:
cout<<"\nEnter The First Complex Number:";
[Link]();
[Link]();
cout<<"\nEnter The Second Complex Number:";
[Link]();
[Link]();
c=a*b;
[Link]();
break;
case 4:
cout<<"\nEnter The First Complex Number:";
[Link]();
[Link]();
cout<<"\nEnter The Second Complex Number:";
[Link]();
[Link]();
c=a/b;
[Link]();
break;
}
}while(ch!=5);
getch();
}
Steps to Overload the Binary Operator to Get the Sum of Two Complex Numbers
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and their member function.
Step 4: Take two numbers using the user-defined inp()function.
Step 6: Similarly, define the binary (-) operator to subtract two numbers.
Step 7: Call the print() function to display the entered numbers.
Step 8: Declare the class objects x1, y1, sum, and sub.
Step 9: Now call the print() function using the x1 and y1 objects.
Step 10: After that, get the object sum and sub result by adding and subtracting the objects using
the '+' and '-' operators.
Step 11: Finally, call the print() and print2() function using the x1, y1, sum, and sub.
Step 12: Display the addition and subtraction of the complex numbers.
Step 13: Stop or terminate the program.
Example 1: Program to perform the addition and subtraction of two complex numbers using the
binary (+) and (-) operator
Let's create a program to calculate the addition and subtraction of two complex numbers by
overloading the '+' and '-' binary operators in the C++ programming language.
/* use binary (+) operator to add two complex numbers. */
#include <iostream>
using namespace std;
class Complex_num
{
// declare data member or variables
int x, y;
public:
// create a member function to take input
void inp()
{
cout << " Input two complex number: " << endl;
cin >> x >> y;
}
// use binary '+' operator to overload
Complex_num operator + (Complex_num obj)
{
// create an object
Complex_num A;
// assign values to object
A.x = x + obj.x;
A.y = y + obj.y;
return (A);
}
// overload the binary (-) operator
Complex_num operator - (Complex_num obj)
{
// create an object
Complex_num A;
// assign values to object
A.x = x - obj.x;
A.y = y - obj.y;
return (A);
}
// display the result of addition
void print()
{
cout << x << " + " << y << "i" << "\n";
}
// display the result of subtraction
void print2()
{
cout << x << " - " << y << "i" << "\n";
}
};
int main ()
{
Complex_num x1, y1, sum, sub; // here we created object of class Addition i.e x1 and y1
// accepting the values
[Link]();
[Link]();
// add the objects
sum = x1 + y1;
sub = x1 - y1; // subtract the complex number
// display user entered values
cout << "\n Entered values are: \n";
cout << " \t";
[Link]();
cout << " \t";
[Link]();
cout << "\n The addition of two complex (real and imaginary) numbers: ";
[Link](); // call print function to display the result of addition
cout << "\n The subtraction of two complex (real and imaginary) numbers: ";
sub.print2(); // call print2 function to display the result of subtraction
return 0;
}
Output
Input two complex numbers:
5
7
Input two complex numbers:
3
5
Entered values are:
5 + 7i
3 + 5i
The addition of two complex (real and imaginary) numbers: 8 + 12i
The subtraction of two complex (real and imaginary) numbers: 2 - 2i
[Link] to object for student mark list
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rollno, no_of_subjects;
char stud_name[20];
int *marks;
public:
student(int rno,char *name, int nos) //Parameterized Constructor
{
rollno=rno;
strcpy(stud_name, name);
no_of_subjects=nos;
marks=new int[no_of_subjects]; //dynamically memory allocated for storing marks
}
void accept();
void display();
};
void student::accept()
{
for(int i=0; i<no_of_subjects; i++)
{
cout<<"\n\n Enter Marks for Subject "<<i+1<<" : ";
cin>>marks[i];
}
}
void student::display()
{
cout<<"\n -----------------------------------------";
cout<<"\n\n Student Information";
cout<<"\n -----------------------------------------";
cout<<"\n\n Roll no : "<<rollno;
cout<<"\n\n Name : "<<stud_name;
for(int i=0; i<no_of_subjects; i++)
{
cout<<"\n\n Marks of Subject "<<i+1<<" : "<<marks[i];
}
cout<<"\n -----------------------------------------\n";
}
int main()
{
int rollno, no_of_subjects;
char stud_name[20];
int cnt, i;
cout<<"\n Enter No. of Students You Want? : ";
cin>>cnt;
for(i=0; i<cnt; i++)
{
cout<<"\n -----------------------------------------\n";
cout<<"\n Enter Roll No : ";
cin>>rollno;
cout<<"\n Enter Name : ";
cin>>stud_name;
cout<<"\n Enter No. of Subjects : ";
cin>>no_of_subjects;
cout<<"\n -----------------------------------------";
student st(rollno, stud_name, no_of_subjects);
[Link]();
[Link]();
}
return 0;
}
Output: