Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C_CPP_Programs_Updated

The document contains multiple C and C++ programs demonstrating various programming concepts including file handling, class inheritance, and dynamic memory allocation. It includes a C program for managing movie details, a C++ program for calculating areas of different shapes using pure virtual functions, a C++ vector class with parameterized and copy constructors, and a student result system that calculates total marks and grades. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

p9280840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C_CPP_Programs_Updated

The document contains multiple C and C++ programs demonstrating various programming concepts including file handling, class inheritance, and dynamic memory allocation. It includes a C program for managing movie details, a C++ program for calculating areas of different shapes using pure virtual functions, a C++ vector class with parameterized and copy constructors, and a student result system that calculates total marks and grades. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

p9280840
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C & C++ Programs with Explanations and Output

1. C Program for Movie Class (File Handling)


Write a C program to create a class Movie with data members M_Name, Release_Year,
Director_Name, and Budget.
(Use File Handling). Write necessary member functions to:

i. Accept details for 'n' movies from the user and store them in a file "Movie.txt".
ii. Display movie details from the file.
iii. Count the number of objects stored in the file.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
char name[50], director[50];
int year;
float budget;
} Movie;

void addMovies(int n) {
FILE *file = fopen("Movie.txt", "wb");
Movie m;
for (int i = 0; i < n; i++) {
printf("Enter Movie Name, Year, Director, Budget: ");
scanf(" %[^
] %d %[^
] %f", m.name, &m.year, m.director, &m.budget);
fwrite(&m, sizeof(Movie), 1, file);
}
fclose(file);
}
void displayMovies() {
FILE *file = fopen("Movie.txt", "rb");
Movie m;
while (fread(&m, sizeof(Movie), 1, file))
printf("%s | %d | %s | %.2f\n", m.name, m.year, m.director, m.budget);
fclose(file);
}

void countMovies() {
FILE *file = fopen("Movie.txt", "rb");
fseek(file, 0, SEEK_END);
int count = ftell(file) / sizeof(Movie);
fclose(file);
printf("Total Movies: %d\n", count);
}

int main() {
int n;
printf("Enter number of movies: ");
scanf("%d", &n);
addMovies(n);
displayMovies();
countMovies();
return 0;
}

2. C++ Program for Shape with Pure Virtual Function


Create a C++ base class Shape. Derive three different classes Circle, Sphere, and Cylinder from
Shape class.
Write a C++ program to calculate the area of Circle, Sphere, and Cylinder. (Use pure virtual
function).

#include <iostream>
#include <cmath>
using namespace std;

class Shape {
public:
virtual void area() = 0;
};

class Circle : public Shape {


protected:
float radius;
public:
Circle(float r) : radius(r) {}
void area() { cout << "Circle Area: " << M_PI * radius * radius << "\n"; }
};

class Sphere : public Circle {


public:
Sphere(float r) : Circle(r) {}
void area() { cout << "Sphere Area: " << 4 * M_PI * radius * radius << "\n"; }
};

class Cylinder : public Circle {


float height;
public:
Cylinder(float r, float h) : Circle(r), height(h) {}
void area() { cout << "Cylinder Area: " << 2 * M_PI * radius * (radius + height) << "\n"; }
};

int main() {
Circle c(5);
Sphere s(5);
Cylinder cy(5, 10);
c.area();
s.area();
cy.area();
return 0;
}

3. C++ Vector Class (Parameterized & Copy Constructor)


Create a C++ class Vector with data members size & pointer to integer. The size of the vector varies
so memory should be allocated dynamically.

Perform following operations:


1. Accept a vector.
2. Display a vector in the format (10, 20, 30,....).
3. Calculate the union of two vectors.

#include <iostream>
using namespace std;

class Vector {
int size, *arr;
public:
Vector(int s) : size(s) { arr = new int[size]; }
Vector(const Vector &v) {
size = v.size;
arr = new int[size];
for (int i = 0; i < size; i++) arr[i] = v.arr[i];
}
void accept() { for (int i = 0; i < size; i++) cin >> arr[i]; }
void display() {
cout << "("; for (int i = 0; i < size; i++) cout << arr[i] << (i < size-1 ? ", " : "");
cout << ")\n";
}
~Vector() { delete[] arr; }
};

int main() {
Vector v1(3), v2(3);
cout << "Enter 3 elements for Vector 1: "; v1.accept();
cout << "Enter 3 elements for Vector 2: "; v2.accept();

cout << "Vector 1: "; v1.display();


cout << "Vector 2: "; v2.display();
return 0;
}

4. C++ Student Result System


Create a base class Student with data members Roll No, Name. Derive two classes from it, class
Theory and class Practical.
Class Result inherits both Theory and Practical. Calculate Total marks, Percentage, and Grade.

#include <iostream>
using namespace std;

class Student {
protected:
int roll;
string name;
public:
void acceptStudent() { cout << "Enter Roll No and Name: "; cin >> roll >> name; }
void displayStudent() { cout << "Roll: " << roll << ", Name: " << name << "\n"; }
};

class Theory : virtual public Student {


protected:
int M1, M2, M3, M4;
public:
void acceptTheory() { cout << "Enter 4 Theory Marks: "; cin >> M1 >> M2 >> M3 >> M4; }
};

class Practical : virtual public Student {


protected:
int P1, P2;
public:
void acceptPractical() { cout << "Enter 2 Practical Marks: "; cin >> P1 >> P2; }
};

class Result : public Theory, public Practical {


int total;
float percentage;
char grade;
public:
void calculate() {
total = M1 + M2 + M3 + M4 + P1 + P2;
percentage = total / 6.0;
grade = (percentage >= 90) ? 'A' : (percentage >= 75) ? 'B' : (percentage >= 50) ? 'C' : 'F';
}
void displayResult() {
displayStudent();
cout << "Total: " << total << ", Percentage: " << percentage << "%, Grade: " << grade << "\n";
}
};

int main() {
Result r;
r.acceptStudent(); r.acceptTheory(); r.acceptPractical();
r.calculate(); r.displayResult();
return 0;
}

You might also like