GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jun-2008, 12:36
khushal_kkk's Avatar
khushal_kkk khushal_kkk is offline
Awaiting Email Confirmation
 
Join Date: Mar 2008
Posts: 50
khushal_kkk is an unknown quantity at this point

Function Overiding?


the code below has some type of mistake that my mind is not working on and the mistake that im getting is that when i cin the data into the human class and wants the data to be entered in student class too it actually first cin the data into the human class and after coutting the result of the human class it offers to enter the data into student class which i want the data to be eneterd at once aand i get the console out at once......please correct my program


my code is as follows:

CPP / C++ / C Code:

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

class Human
{
	 private:
		 char name[40];
		 char gender[8];
		 int day,month,year;
	  public:
		Human()
		{
		   strcpy(name,"\0");
		   strcpy(gender,"\0");
		   day=month=year=0;
		}
		void ReadData()
		{
		  cout<<endl<<"Name ? ";gets(name);
		  cout<<"Gender ? ";cin>>gender;
		  cout<<"Day/Month/Year ? ";cin>>day>>month>>year;
		 }
		 void ShowData()
		 {
		   cout<<endl<<"Name : "<<name;
		   cout<<endl<<"Gender : "<<gender;
		   cout<<endl<<"Date of Birth: "<<day<<"/"<<month<<"/"<<year;
		 }
	  };
	 class Student:public Human
	 {
		 private:
			 int rno;
			 char group[8];
			 int semester;
	  public:
			 Student()
			 {
				rno=semester=0;
				strcpy(group,"\0");
			 }
			 void Show()
			 {

				 cout<<endl<<"Enter Roll Number :";cin>>rno;
				 cout<<"Enter The Group :";cin>>group;
				 cout<<"Enter The Semester :";cin>>semester;
			  }
			 void Read()
			 {
			 cout<<endl<<"Roll Number :";
			 cout<<endl<<"Group :";
			 cout<<endl<<"Semester :";
			 }
		 };
		 int main()
		 {
			clrscr();
			Student s1;
			s1.Read();
			s1.ReadData();
			s1.Show();
			s1.ShowData();

		 cout<<endl<<"The Bytes Reserved In Memory For The Whole Program is :"<<sizeof(s1);
			getch();
			return 0;
		 }





sorry for mistakes done i hurry....Regards
  #2  
Old 02-Jun-2008, 20:57
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Function Over Ridding ...?


Quote:
Originally Posted by khushal_kkk
the mistake that im getting is that when i cin the data into the human class and wants the data to be entered in student class too...
What you are wanting to do is initialize all members in the derived class instance as once. Typically, this is done by initializing the base class members in the base class constructor, & initializing the derived class members in the derived class constructor.

In the code presented, you should be calling the ReadData() member function within the Human class's constructor, & Read() member function within the Student class's constructor.

By doing this, you will be able to significantly reduce the number of member functions called in main().
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.