0% found this document useful (0 votes)
89 views

Lab 6 Details of N Students. / C++ Program To Demonstrate Example of Array of Objects

This document contains 3 C++ programming examples: 1. A program to create a class for students that gets and prints details like name, roll number, total marks, and percentage for an array of student objects. 2. A program demonstrating constant functions and objects by creating a constant Demo object and printing its value. 3. A program defining a BankAccount class with data members like name, account number, type, and balance. Member functions are written to assign initial values, deposit, withdraw, and display details. A main function tests the class.
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)
89 views

Lab 6 Details of N Students. / C++ Program To Demonstrate Example of Array of Objects

This document contains 3 C++ programming examples: 1. A program to create a class for students that gets and prints details like name, roll number, total marks, and percentage for an array of student objects. 2. A program demonstrating constant functions and objects by creating a constant Demo object and printing its value. 3. A program defining a BankAccount class with data members like name, account number, type, and balance. Member functions are written to assign initial values, deposit, withdraw, and display details. A main function tests the class.
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
You are on page 1/ 6

Lab 6

1. C++ program to create a class for student to get and print


details of N students. / C++ program to demonstrate
example of array of objects.
#include <iostream>
using namespace std;

#define MAX 10

class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:

void getDetails(void);

void putDetails(void);
};

void student::getDetails(void){
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;

perc=(float)total/500*100;
}

void student::putDetails(void){
cout << "Student details:\n";
cout << "Name:"<< name << ",Roll Number:" << rollNo
<< ",Total:" << total << ",Percentage:" << perc;
}

int main()
{
student std[MAX];
int n,loop;

cout << "Enter total number of students: ";


cin >> n;
for(loop=0;loop< n; loop++){
cout << "Enter details of student " << loop+1 << ":\n";
std[loop].getDetails();
}

cout << endl;

for(loop=0;loop< n; loop++){
cout << "Details of student " << (loop+1) << ":\n";
std[loop].putDetails();
}

return 0;
}

2. Write a programme for constant function and object.


#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}

3. Define a class to represent a bank account. Include the


following members:

Data members:
1) Name of the depositor
2) Account number
3) Type of account
4) Balance amount in the account.

Member functions:
1) To assign initial values
2) To deposit an amount
3) To withdraw an amount after checking the balance
4) To display name and balance.

Write a main program to test the program.


#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

class bank
{
int a;
char nm[100], acctype[100];
float bal;
public:
bank(int a_n, char *name, char *acc_type, float
balance)
{
a=a_n;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit()
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw()
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display()
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<a;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;
}
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;

bank b1(a_n, name, acc_type, balance);


b1.deposit();
b1.withdraw();
b1.display();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy