What's wrong in my c coding (constructors)?

  • Thread starter Dr.Brain
  • Start date
  • Tags
    Coding
In summary, a constructor in C is declared using the struct keyword and parameters, and is used to initialize members and perform setup tasks for an object. It is called using the struct keyword and arguments, and does not have a return type. If not declared, a default constructor is generated.
  • #1
Dr.Brain
538
2
#include<stdio.h>
#include<conio.h>
class info
{
int name,roll;
public:
void input();
void output();
}
void info::input()
{
printf("Enter your name and rollno \n");
scanf("%c%d" ,&name,&roll);
}
void info::eek:utput()
{
printf("Your name is %c \n" ,name);
printf("Your rollno is &d \n" , roll);
}
class marks :public info
{
int math,bio,eng;
public:
void input();
void output();
}
void marks::input()
{
info::input() ;
printf("Input marks : \n");
scanf("%d%d%d" , &math , &bio , &eng);
}
void marks::eek:utput()
{
info::eek:uput();
printf("Your marks are %d %d %d \n" ,math,bio,eng);
}
void main()
{
clrscr();
class marks m;
m.input();
m.output();
getch();
}

---------------

I have darkened the line in which my compiler shows the error...i don't seem to find any ... :bugeye: , it says "Declaration Syntax Error")
 
Technology news on Phys.org
  • #2
Are you using a C++ compiler or a C compiler?
 
  • #3
Is 'info' a reserved word? Try changing it.
 
  • #4
Put a semicolon after the class declarations:
Code:
class info
{
int name,roll;
public:
void input();
void output();
};
etc.

You need a C++ compiler for this.
 
  • #5
heh, my program would look like this:

Code:
using namespace std;
while(Subject.GetNext())
{
   cout >> "You'r Grade for ">> Subject.Get() >> " is ";
   cout >> "F!" >> endl;
}
 
Last edited:
  • #6
3trQN said:
heh, my program would look like this:

Code:
using namespace std;
while(Subject.GetNext())
{
   cout >> "You'r Grade for ">> Subject.Get() >> " is "
   cout >> "F!" >> endl;
}

Too bad it won't compile.
 
  • #7
do constructors worl only in C++ and not C?
 
  • #8
C does not have classes. A C compiler will not recognize the class keyword.

C does have structures. In C++, a structure can have a constructor as well as member functions. I do not know whether structs in C can have constructors or other member functions.

Good luck,
Tim
 
  • #9
C structs(different from C++ structs) cannot have functions...they can have function pointers but not functions.
 

Related to What's wrong in my c coding (constructors)?

1. How do I declare a constructor in my C code?

A constructor in C is declared using the struct keyword, followed by the name of the structure, and then parentheses containing the parameters for the constructor. For example: struct Person(firstName, lastName, age)

2. What is the purpose of a constructor in C?

A constructor in C is used to initialize the members of a structure or class when an object is created. It can also perform any necessary setup tasks for the object.

3. How do I call a constructor in my C code?

In C, a constructor is called by using the struct keyword followed by the name of the structure, and then parentheses containing the arguments for the constructor. For example: struct Person p = Person("John", "Doe", 25);

4. Can a constructor have a return type in C?

No, a constructor in C does not have a return type. It implicitly returns a pointer to the newly created object.

5. What happens if I don't declare a constructor in my C code?

If a constructor is not declared in a structure or class in C, the compiler will automatically generate a default constructor that does not take any arguments or perform any initialization. However, it is recommended to declare a custom constructor in order to have more control over the initialization process.

Similar threads

Replies
4
Views
878
Replies
3
Views
936
Replies
1
Views
2K
Replies
2
Views
1K
Replies
36
Views
2K
Replies
89
Views
5K
Replies
3
Views
2K
Back
Top