Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C++
//program
#include<iostream.h>
class student
{
int rollno;
char name[20];
int marks;
public:
student(int rollno,char name[20],int marks);
student(student &s)
{
rollno=s.rollno;
name==s.name;
marks=s.marks;
}
void display()
{
cout<<rollno<<name<<marks;
}
};
void main()
{  str="hka";
student stud1(23,str,100);
student stud2(stud1);
stud2.display();
}

Pls tell me where i have to correct my code..

What I have tried:

Tried to initialize the string
Posted
Updated 19-Sep-18 1:57am
v3

You must implement the first student constructor. Class names are starting with a capital letter.

Ans the "str" variable isnt defined. Else read the the error messages with more care and google for the error number. Mostly they are understandable.

Visit some tutorials like the Learn CC++ tutorial for learning more about programming, like code syntax and debugging.
 
Share this answer
 
Comments
CPallini 19-Sep-18 8:06am    
No, Class names don't need to be capitalized.
Try this:

C++
//program
#include <iostream>

class student
{
private:
	int rollno;
	char name[20];
	int marks;

public:
	student(int _rollno, const char* _name, int _marks)
	{
		memset(name, 0, 19);
		rollno = _rollno;
		strcpy_s(name, _name);
		marks = _marks;
	}

	student(student &s)
	{
		rollno = s.rollno;
		strcpy_s(name, s.name);
		marks = s.marks;
	}

	void display()
	{
		std::cout << rollno << ", " << name << ", " << marks;
	}
};

void main()
{
	const char *str = "hka\0";
	
	student stud1(23, str, 100);
	student stud2(stud1);
	stud2.display();
}
 
Share this answer
 
Comments
H.K.A2 21-Sep-18 11:40am    
Thanks a lot for your help.. But what is memset?
Leo Chapiro 21-Sep-18 13:26pm    
memset(void *str, int c, size_t n) copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str.
This is just an initialisation of "name" with 0.
What compiler are you using? Turboc++ 3.0? If so, start with ditching it.
cout is in std namespace.
Use iostream not iostream.h,
you need to declare constructor body
Your str is not declared
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900