Q:

C++ Class Exercise - Read and Print Class, Students Details using Two Classes

belongs to collection: C++ programs on various topics

0

C++ Class Exercise - Read and Print Class, Students Details using Two Classes

All Answers

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

C++ program - Read and print Class, Student details using Two Classes

Let’s consider the following example:

/*C++ Class Exercise - Read and Print Class, Students 
Details using Two Classes*/

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

using namespace std;

class student{
	private:
		char name[30];
		int rollNo;
	public:
		void getStudent(){
			strcpy(name,"PIYA KAUSHAL");
			rollNo=101;
		}
		void printStudent(){
			cout<<"Name: "<<name<<",Roll No.: "<<rollNo<<endl;
		}
};

class classDetails{
	private:
		char clsName[30];
		student std; //object
	public:
		void getClassDetails(){
			strcpy(clsName,"Higher Sec.");
			std.getStudent();			
		}
		void printClassDetails(){
			cout<<"Class Name: "<<clsName<<endl;
			std.printStudent();
		}
};

int main()
{
	classDetails CD;
	CD.getClassDetails();
	CD.printClassDetails();
	return 0;
}

Output

    Class Name: Higher Sec.
    Name: PIYA KAUSHAL,Roll No.: 101

 

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

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Class Exercise - Read and Print House details ... >>
<< C++ program for Constructor and Destructor Declara...