answersLogoWhite

0


Best Answer

structure variable can access the variable

but class object can access the function also

User Avatar

Wiki User

17y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between struct variable and class object?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Math & Arithmetic

What is the difference between declaring variable and initializing variables?

Actually, there is a third step, call definition. Declaration is a statement to the compiler of what type an identifier is, definition is the allocation of memory for that identifier, and initialization is the assignment of an initial value to that identifier. Usually, declaration and definition are done together, but you can also add initialization in that step if desired. int a; /* declaration and definition */ a = 1; /* initialization */ int a = 1; /* declaration, definition, and initialization */ For the case of seperate declaration and definition, consider the struct... struct _mystruct { int a; }; /*declaration */ struct _mystruct mystruct; /* definition */ struct _mystruct { int a; } mystruct; /*declaration and definition */ Note: To be more precise: struct _mystruct; /* struct declaration */ struct _mystruct { int a; }; /* struct definition */ typedef struct _mystruct MYTYPE; /* type definition */ extern struct _mystruct mystructvar; /* variable declaration */ struct _mystruct mystructvar; /* variable definition */ struct _mystruct mystructvar = {7} ; /* variable definition with initialization */ struct _mystruct { int a; } mystruct; /* struct definition and variable definition */ extern struct _mystruct { int a; } mystruct; /* struct definition and variable declaration */


What is the c plus plus program to find the area and perimeter of a circle and rectangle?

#include<iostream> struct shape { virtual double perimeter() const = 0; virtual double area() const = 0; }; struct circle : shape { circle (double radius): r(radius), pi(4*atan(1)) {} double perimeter() const { return 2*pi*r; } double area() const { return pi*r*r; } private: double r; const double pi; }; struct rectangle : shape { rectangle (double width, double height): w(width), h(height) {} double perimeter() const { return 2*(w+h); } double area() const { return w*h; } private: double w; double h; }; int main() { using std::cout; using std::endl; circle c (3.0); cout << "Circle with radius 3.0\n"; cout << "\tPerimiter: " << c.perimiter() << '\n'; cout << "\tArea: " << c.area() << '\n'; cout << endl; rectangle r (4.0, 5.0); cout << "Rectangle with width 4.0 and height 5.0\n"; cout << "\tPerimiter: " << r.perimiter() << '\n'; cout << "\tArea: " << r.area() << '\n'; cout << endl; }


How do you write a program in C that calculates the area of a triangle given the three x and y coordinates of each corner of the triangle Alongside it must make use of structs.?

To calculate the area of a triangle you needto get the coordinates of the points of the triangles A = (xa, ya), etc (into a structure, eg user input and parsing, direct assignment in the program, etc)calculate area = abs( (xb*ya - xa*yb) + (xc*yb - xb*yc) + (xa*yc - xc*ya) ) / 2put the answer somewhere (eg onto stdout, in the struct for future use, etc).Your structure would include (at a minimum) the coordinates of the points, eg:struct triangle{int xa, ya;int xb, yb;int xc, yc;};then you would declare a structure to hold the points and use it to calculate the area, eg:struct triangle t1;t1.xa = 0; t1.ya = 0;t1.xb = 5; t1.yb = 5;t1,xc = 7; t1.yc = 7;area = calculate_area(&t1);To calculate the area, you could put it into a function: calculate_area(tri)struct triangle *tru;{ area;area = ()abs( (tri->xb * tri->ya - tri->xa * ... )) /* formula above */return area;}I'll let you think about:what the return type ought to be;what type to use to store the triangle's points;any potential error trapping that may need to be done;what compiler options (and header files) you may need.and put the program together.Exactly what data you want to store in the struct (minimum the points, but you may want to store the area of the triangle as well - in future you could expand to include calculating the side lengths and angles)


Write a c program to find inverse of a matrix?

#include<stdio.h> #include<math.h> float detrm(float[][],float); void cofact(float[][],float); void trans(float[][],float[][],float); main() { float a[25][25],k,d; int i,j; printf("ENTER THE ORDER OF THE MATRIX:\n"); scanf("%f",&k); printf("ENTER THE ELEMENTS OF THE MATRIX:\n"); for(i=0;i<k;i++) { for(j=0;j<k;j++) { scanf("%f",&a[i][j]); } } d=detrm(a,k); printf("THE DETERMINANT IS=%f",d); if(d==0) printf("\nMATRIX IS NOT INVERSIBLE\n"); else cofact(a,k); } /******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/ float detrm(float a[25][25],float k) { float s=1,det=0,b[25][25]; int i,j,m,n,c; if(k==1) { return(a[0][0]); } else { det=0; for(c=0;c<k;c++) { m=0; n=0; for(i=0;i<k;i++) { for(j=0;j<k;j++) { b[i][j]=0; if(i!=0&&j!=c) { b[m][n]=a[i][j]; if(n<(k-2)) n++; else { n=0; m++; } } } } det=det+s*(a[0][c]*detrm(b,k-1)); s=-1*s; } } return(det); } /*******************FUNCTION TO FIND COFACTOR*********************************/ void cofact(float num[25][25],float f) { float b[25][25],fac[25][25]; int p,q,m,n,i,j; for(q=0;q<f;q++) { for(p=0;p<f;p++) { m=0; n=0; for(i=0;i<f;i++) { for(j=0;j<f;j++) { b[i][j]=0; if(i!=q&&j!=p) { b[m][n]=num[i][j]; if(n<(f-2)) n++; else { n=0; m++; } } } } fac[q][p]=pow(-1,q+p)*detrm(b,f-1); } } trans(num,fac,f); } /*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/ void trans(float num[25][25],float fac[25][25],float r) { int i,j; float b[25][25],inv[25][25],d; for(i=0;i<r;i++) { for(j=0;j<r;j++) { b[i][j]=fac[j][i]; } } d=detrm(num,r); inv[i][j]=0; for(i=0;i<r;i++) { for(j=0;j<r;j++) { inv[i][j]=b[i][j]/d; } } printf("\nTHE INVERSE OF THE MATRIX:\n"); for(i=0;i<r;i++) { for(j=0;j<r;j++) { printf("\t%f",inv[i][j]); } printf("\n"); } } ALTERNATIVE CODE: #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> #include<math.h> // Written by Ran // There have been enough comments to help the reader easily understand this program // Helpfulness of COMMENTS in this program and Pre-requisites:- // a. However it's assumed that the reader is familiar with the basics of C++ // b. It is also assumed that the reader knows the basic mathematics involving matrices. // c. Since this program focusses on how to find inverse of a matrix, the comments // in the program are sufficient for understanding this. // It is assumed that the reader is familiar with basics of matrices in C++ (like input, display, // addition,transpose,etc. of matrices) // The comments in this program aim to explain the reader how to find inverse // d. Hence through comments, the reader will also be taught how to find determinant, // adjoint, cofactor,etc. However as said in the previous lines, there won't be comments // for explaining basics like input,display,etc of a matrix using C++. // NOTES: // 1. float datatype has been used for matrix, determinant. // 2. To have consistency between mathematics and C++, this program considers a[1][1] as the first element // i.e row and column indices begin with 1 same as mathematics. // Define a structure matrix with a matrix (2D array of type float) and size n // Declare the objects of this structure used in this program struct matrix { float a[25][25]; int n; }obj,c_obj,trans_obj,obj_cof,obj_adj,obj_inv; // Prototypes of the functions used in this program void input(matrix&); void display(matrix&); matrix reduced(matrix &, int ,int ); float determinant(matrix); float cofactor(matrix,int,int); matrix transpose(matrix); matrix adjoint(matrix); matrix inverse(matrix obj); // Begining of Main function int main() { // Getting dimensions input by the user int r,c; again: cout<<"Enter the order of the matrix: "<<endl; cout<<"Enter Row dimension: "; cin>>r; cout<<"Enter Column dimension: "; cin>>c; // Check dimensions for square matrix so that inverse can be found // If user enters different dimensions for row and column, ask to re-enter or quit program if(r!=c) { char ans; cout<<"Inverse can be found out only for a square matrix. Enter same dimension for row and column. Do you want to enter the dimensions again? Press Y for yes"<<endl; cin>>ans; if(ans=='y') goto again; else cout<<"Program exit"; getch(); exit(0); } // If it's a square matrix, proceed else if(r==c) {obj.n=r;} cout<<endl; input(obj); // call input function to input the matrix elements from the user display(obj); // display the matrix got as input now // Following lines were used to test parts/sections/segments of the code and hence commented /* char ans2; cout<<"do u want to check reduce matrix? Press y to check reduce matrix and press any char to skip this"<<endl; cin>>ans2; if(ans2=='y') { int i,j; cout<<"Enter row i and col j to get reduced matrix"<<endl; cin>>i>>j; // i=i-1; // j=j-1; c_obj=reduced(obj,i,j); char ans1; cout<<"Do you want to display the reduced matrix? If yes, Press y "<<endl; cin>>ans1; if(ans1=='y') { cout<<"Displaying reduced matrix..."<<endl; display(c_obj); } }*/ //Find Determinant cout<<"Finding determinant......"<<endl; cout<<"The determinant is"<<determinant(obj)<<endl; //Find Cofactor if user wishes to char ans3; cout<<"Do you want to find cofactor? Press y if yes"<<endl; cin>>ans3; while(ans3=='y') { int i,j; cout<<"Finding cofactor. Enter row and column"<<endl; cin>>i>>j; cout<<"Cofactor of a["<<i<<"]["<<j<<"] is "<<cofactor(obj,i,j)<<endl; cout<<"want of find cofactor of another element? Press y for yes"<<endl; cin>>ans3; } // Following lines were to meant to test ONLY the transpose function and hence commented /* cout<<"Printing Transpose of the matrix "<<endl; matrix trans1; trans1=transpose(obj); display(trans1); */ // Find Inverse cout<<"\n\n\n Finding Inverse. . .\n\n"; matrix obj_inv2; obj_inv2=inverse(obj); display(obj_inv2); // Display the matrix inverse getch(); return 0; } void input(matrix &obj) { // This function gets elements of a matrix input by the user // Parameter is the structure object obj (used throughout the program) // Parameter is "passed by reference" so as to reflect the changes made by this function, to other functions that call it cout<<"Enter the matrix "<<endl; for (int i=1;i<=obj.n;i++) { for (int j=1;j<=obj.n;j++) { cout<<"Enter the element a["<<i<<"]["<<j<<"] : "; cout<<endl; cin>>obj.a[i][j]; } } } void display(matrix &obj) { // This function displays elements of a matrix passed to it as a parameter // Parameter is the structure object obj (used throughout the program) // Parameter is "passed by reference" but may be "passed by value" also. if(obj.n==0) return; else{ cout<<"The matrix is: "<<endl; for (int i=1;i<=obj.n;i++) { for (int j=1;j<=obj.n;j++) { cout<<obj.a[i][j]<<" "; } cout<<endl; } }} matrix reduced(matrix &obj, int i,int j) { // This function reduces the matrix passed as input to it // The 'reduction' requirement is like this: // Eliminate the row i and column j from the given matrix to get the reduced matrix // This is done by the following logic: // a is given matrix. c_obj is desired reduced matrix // i. Using two for loops (iterating with p and q here) as usual, we scan the given matrix. // row and col represent the current location pointer of row and column of the required reduced matrix. // ii. All elements from given matrix are copied to reduced matrix except for those corresponding to // row i and column j // iii. The reduced matrix has its dimensions one less than that of given matrix int row=1,col=1; for(int p=1;p<=obj.n;p++) // outer loop traverses through rows as usual { for(int q=1;q<=obj.n;q++) // inner loop traverses through columns as usual { if((p!=i)&&(q!=j)) // Skip the elements corresponding to row i OR column j of the given matrix { c_obj.a[row][col]=obj.a[p][q]; col=col+1; } if(col>=obj.n) // When column 'col' of reduced matrix reaches (or exceeds n), reset it to 1 { // and increment 'row'. This means current row of reduced matrix got filled and // we need to begin filling a new row. col=1; row=row+1; if (row>=obj.n) //This represents the case when both 'col' and 'row of reduce matrix reach (or // exceed) n. This means the reduced matrix has been filled up.Break out of the loops. break; } } } c_obj.n=obj.n-1; // Fix the dimension of the reduced matrix one less than the given input matrix return c_obj; // Return the reduced matrix to the calling function. } float determinant(matrix obj) { // This function is called recursively until we get dimension = 1 where the only element in the matrix gets returned. float det=0; if(obj.n==1) {return obj.a[1][1]; } else { for(int scan=1;scan<=obj.n;scan++)//Fix the first row and vary the column in this row using for loop iteration variable 'scan' { det=det+obj.a[1][scan]*int(pow(-1,(1+scan)))*determinant(reduced(obj,1,scan)); // det is calculated to be the sum of the following // i. prev value stored in det. // ii. current element in the first row (i.e. a[1][scan]) MULTIPLIED by -1^(i+j) [i is 1 for 1st row and j is nothing but scan here MULTIPLIED by the reduced matrix corresponding to this i (1) and j (scan) // PLEASE UNDERSTAND BY COMPARING THIS WITH THE MATHEMATICAL WAY OF CALCULATING DETERMINANT // - It's computed in a similar way here. } return det; } } float cofactor(matrix obj,int i,int j) { // The computation done here is like this: // If the matrix (passed as paramenter) has dimension = 1, return the only element as cofactor // Else, return determinant of the reduced matrix corresponding to i and j passed with a // multiplication factor -1^(i+j) float cofact; if(obj.n==1) { return obj.a[1][1]; } else { cofact=int(pow(-1,(i+j)))*determinant(reduced(obj,i,j)); } return cofact; } matrix transpose(matrix obj) { // Transpose matrix is the given matrix with its rows and columns interchanged. // Just invert the elements during storing when scanning through the for loops // trans_obj is the transposed matrix, returned by the function. // obj is the input matrix passed to this function. trans_obj.n=obj.n; for(int i=1;i<=obj.n;i++) { for(int j=1;j<=obj.n;j++) { trans_obj.a[i][j]=obj.a[j][i]; } } return trans_obj; } matrix adjoint(matrix obj) { // obj_adj is adjoint matrix and obj_cof is cofactor matrix // both have dimensions n // Cofactor matrix of a given matrix is a matrix whose elements are the cofactors of the respective // elements of the given matrix // Adjoint matrix is transpose of cofactor matrix. Return this obj_adj.n=obj.n; obj_cof.n=obj.n; for(int i=1;i<=obj.n;i++) { for(int j=1;j<=obj.n;j++) { obj_cof.a[i][j]=cofactor(obj,i,j); } } obj_adj=transpose(obj_cof); return obj_adj; } matrix inverse(matrix obj) { // Formula : Inverse of a matrix is = adj(matrix)/its determinant float d=determinant(obj); // First find determinant of the given matrix matrix obj_null; obj_null.n=0; // Display error message if determinant is 0 if(d==0) { cout<<"Inverse can be found only if the determninant of the matrix is non-zero"<<endl; return obj_null; } // Determinant is non-zero - Proceed finding inverse using the above formula else { matrix obj_adj1=adjoint(obj); obj_adj1.n=obj.n; obj_inv.n=obj.n; for(int i=1;i<=obj.n;i++) { for(int j=1;j<=obj.n;j++) { obj_inv.a[i][j]=(obj_adj1.a[i][j])/d; } } return obj_inv; } }


Related questions

What is the difference between declaring variable and initializing variables?

Actually, there is a third step, call definition. Declaration is a statement to the compiler of what type an identifier is, definition is the allocation of memory for that identifier, and initialization is the assignment of an initial value to that identifier. Usually, declaration and definition are done together, but you can also add initialization in that step if desired. int a; /* declaration and definition */ a = 1; /* initialization */ int a = 1; /* declaration, definition, and initialization */ For the case of seperate declaration and definition, consider the struct... struct _mystruct { int a; }; /*declaration */ struct _mystruct mystruct; /* definition */ struct _mystruct { int a; } mystruct; /*declaration and definition */ Note: To be more precise: struct _mystruct; /* struct declaration */ struct _mystruct { int a; }; /* struct definition */ typedef struct _mystruct MYTYPE; /* type definition */ extern struct _mystruct mystructvar; /* variable declaration */ struct _mystruct mystructvar; /* variable definition */ struct _mystruct mystructvar = {7} ; /* variable definition with initialization */ struct _mystruct { int a; } mystruct; /* struct definition and variable definition */ extern struct _mystruct { int a; } mystruct; /* struct definition and variable declaration */


Can a DateTime variable be null?

hi i can say that datetime cant be null because datetime is valuetyp(struct) its not referece type(object)


Which of the following is not a variable data type real integer number or string?

FILE, struct stat and struct tm are some examples.


Which data structure in a compiler is responsible for managing information about variables and their attributes?

It is up to the designer of the compiler to decide... it can be something like this: struct Type; struct Block; typedef struct Variable { const char *name; struct Type *type; struct Block *block; /* refers to the block that is the scope of the variable */ int attributes; /* volatile, const, static etc */ } Variable;


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


What is the difference between class and struct?

The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive. When we create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized. It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values. It is an error to initialize an instance field in a struct. There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do. A struct is a value type, while a class is a reference type.


What is the difference between struct and union in c file management?

The main difference is in how the data structures are stored. In a union, all of the elements are stored in one location. A structure stores each of its elements in a separate memory location.


What is typdef in c?

It is a keyword generally used to rename data types, using typedef you can create alias which can be used to declare the variable. e.g. typedef struct node { int info; struct node *next; }Node; now to declare variable of struct node type we can use the word Node in place of struct node


How is a structure type pointer variable declared?

struct thisorthat *ptr;


How is structure type pointer variable declared?

struct thisorthat *ptr;


What is typdef in c language?

It is a keyword generally used to rename data types, using typedef you can create alias which can be used to declare the variable. e.g. typedef struct node { int info; struct node *next; }Node; now to declare variable of struct node type we can use the word Node in place of struct node


How is a structure type pointer variable declared in c?

struct thisorthat *ptr;