Q:

Create an object of a class inside another class declaration in C++

0

Create an object of a class inside another class declaration in C++

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

using namespace std;

class Marks
{
	private:
		int rno;
		float perc;
	
	public:
		//constructor
		Marks()
		{rno = 0; perc = 0.0f;}
		
		//input roll numbers and percentage
		void readMarks(void)
		{
			cout<<"Enter roll number: ";
			cin>>rno;
			cout<<"Enter percentage: ";
			cin>>perc;
		}
		
		//print roll number and percentage
		void printMarks(void)
		{
			cout<<"Roll No.: "<<rno<<endl;
			cout<<"Percentage: "<<perc<<"%"<<endl;
		}
};

class Student
{
	private:
		//object to Marks class
		Marks objM;
		char name[30];
	
	public:
		//input student details
		void readStudent(void)
		{
			//Input name
			cout<<"Enter name: ";
			cin.getline(name, 30);
			//input Marks
			objM.readMarks();			
		}
		
		//print student details
		void printStudent(void)
		{
			//print name
			cout<<"Name: "<<name<<endl;
			//print marks
			objM.printMarks();
		}
};

//main code
int main()
{
	//create object to student class
	Student std;
	std.readStudent();
	std.printStudent();
	
	return 0;
}

Output

Enter name: Amit Shukla
Enter roll number: 101
Enter percentage: 84.02
Name: Amit Shukla
Roll No.: 101
Percentage: 84.02%

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now